引擎版本为3.12
Android
1.在游戏的Cocos2dxactivity中编写一个静态方法:
/**
* 设置横竖屏
* @param orientation
*/
public static void changedActivityOrientation(int orientation){
Activity currAppActivity = (Activity) AppActivity.getContext();
switch(orientation)
{
case 1://横屏
currAppActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case 2://竖屏
currAppActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
}
2.在js代码中调用这个段java代码,这里我用的是jsb.reflection.callStaticMethod来调用的,看一下代码:
/**
* orientation int 1:横屏 2:竖屏
*/
deviceUtil.setScreenOrientation=function(orientation){
if(cc.sys.os==cc.sys.OS_ANDROID){
jsb.reflection.callStaticMethod(jsCallJavaMethod.CLASSNAME,"changedActivityOrientation","(I)V", orientation); //调用java代码
if (orientation == 1) {
var glView = cc.director.getOpenGLView();
var frame = glView.getFrameSize();
glView.setFrameSize(frame.height, frame.width);
cc.director.getOpenGLView().setDesignResolutionSize(1280,720,cc.ResolutionPolicy.EXACT_FIT);
}else if (orientation == 2) {
var glView = cc.director.getOpenGLView();
var frame = glView.getFrameSize();
glView.setFrameSize(frame.height, frame.width);
cc.director.getOpenGLView().setDesignResolutionSize(720,1280,cc.ResolutionPolicy.EXACT_FIT);
}
}
}
deviceUtil.js是一个全局文件
通过以上两步就可以在Android平台通过代码动态的控制游戏中横竖屏的切换。
IOS
1.在AppController.mm下声明一个静态变量rvc,如下:
static RootViewController *rvc;
2.然后在didFinishLaunchingWithOptions这个方法中初始化这个静态变量,如下:
rvc = viewController; //把RootViewController的实例化对象复制给我们的静态变量,供下面使用。
3.在AppController.mm中编写两个静态方法,用来调整苹果下的屏幕切换
/**
* 转屏 orientation 1:横屏 2:竖屏
**/
+ (void) changeScreenOrientationHorizontal{
//旋转屏幕,但是只旋转当前的View
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
rvc.view.transform = CGAffineTransformMakeRotation(M_PI/2);
CGRect frame_v = rvc.view.bounds;
rvc.view.bounds = CGRectMake(0, 0, frame_v.size.height, frame_v.size.width);
}
+ (void) changeScreenOrientationVertical{
//旋转屏幕,但是只旋转当前的View
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
rvc.view.transform = CGAffineTransformMakeRotation(M_PI*2);
CGRect frame_v = rvc.view.bounds;
rvc.view.bounds = CGRectMake(0, 0, frame_v.size.height, frame_v.size.width);
}
4.在js代码中调用OC的静态方法:
/*
orientation int 1:横屏 2:竖屏
*/
deviceUtil.setScreenOrientation=function(orientation){
if (cc.sys.os==cc.sys.OS_IOS) {
if (orientation == 1) {
jsb.reflection.callStaticMethod("AppController", "changeScreenOrientationHorizontal"); //调用OC代码
var glView = cc.director.getOpenGLView();
var frame = glView.getFrameSize();
glView.setFrameSize(frame.height, frame.width);
cc.director.getOpenGLView().setDesignResolutionSize(1280,720,cc.ResolutionPolicy.EXACT_FIT);
}else if (orientation == 2) {
jsb.reflection.callStaticMethod("AppController", "changeScreenOrientationVertical");
var glView = cc.director.getOpenGLView();
var frame = glView.getFrameSize();
cc.log(JSON.stringify(frame));
glView.setFrameSize(frame.height, frame.width);
cc.director.getOpenGLView().setDesignResolutionSize(720,1280,cc.ResolutionPolicy.EXACT_FIT);
}
}
}
完成以上的内容,就可以实现ios下屏幕自动切换。现在存在的一点问题是在模拟器上看,切换有点卡顿,不知道真机上有没有问题。这个问题在Android真机上是不存在的。