引擎版本為3.12
Android
1.在游戲的Cocos2dxactivity中編寫(xiě)一個(gè)靜態(tài)方法:
/**
* 設(shè)置橫豎屏
* @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代碼中調(diào)用這個(gè)段java代碼,這里我用的是jsb.reflection.callStaticMethod來(lái)調(diào)用的如捅,看一下代碼:
/**
* 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); //調(diào)用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是一個(gè)全局文件
通過(guò)以上兩步就可以在Android平臺(tái)通過(guò)代碼動(dòng)態(tài)的控制游戲中橫豎屏的切換虎囚。
IOS
1.在AppController.mm下聲明一個(gè)靜態(tài)變量rvc,如下:
static RootViewController *rvc;
2.然后在didFinishLaunchingWithOptions這個(gè)方法中初始化這個(gè)靜態(tài)變量五慈,如下:
rvc = viewController; //把RootViewController的實(shí)例化對(duì)象復(fù)制給我們的靜態(tài)變量纳寂,供下面使用主穗。
3.在AppController.mm中編寫(xiě)兩個(gè)靜態(tài)方法,用來(lái)調(diào)整蘋(píng)果下的屏幕切換
/**
* 轉(zhuǎn)屏 orientation 1:橫屏 2:豎屏
**/
+ (void) changeScreenOrientationHorizontal{
//旋轉(zhuǎn)屏幕毙芜,但是只旋轉(zhuǎn)當(dāng)前的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{
//旋轉(zhuǎn)屏幕忽媒,但是只旋轉(zhuǎn)當(dāng)前的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代碼中調(diào)用OC的靜態(tài)方法:
/*
orientation int 1:橫屏 2:豎屏
*/
deviceUtil.setScreenOrientation=function(orientation){
if (cc.sys.os==cc.sys.OS_IOS) {
if (orientation == 1) {
jsb.reflection.callStaticMethod("AppController", "changeScreenOrientationHorizontal"); //調(diào)用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);
}
}
}
完成以上的內(nèi)容,就可以實(shí)現(xiàn)ios下屏幕自動(dòng)切換∫钢啵現(xiàn)在存在的一點(diǎn)問(wèn)題是在模擬器上看晦雨,切換有點(diǎn)卡頓,不知道真機(jī)上有沒(méi)有問(wèn)題隘冲。這個(gè)問(wèn)題在Android真機(jī)上是不存在的闹瞧。