cocos2d-x遊戲開發屏幕橫豎屏切換

android解決方案:android

1.在遊戲的主activity中編寫一個靜態方法(繼承Cocos2dxActivity)函數

public static void changedActivityOrientation(int orientation){
switch(orientation)
{
case 1://橫屏
instance.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case 2://豎屏
instance.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
}測試

2.在須要切換橫豎屏的C++代碼中經過JNI調用changedActivityOrientation方法,以下所示spa

//切換豎屏代碼 xml

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo minfo;
if( JniHelper::getStaticMethodInfo(minfo,"org/cocos2dx/zylgame/CzmjGame","changedActivityOrientation","(I)V") )
{
minfo.env->CallStaticVoidMethod(minfo.classID,minfo.methodID,1);
}
CCEGLView *pEGLView = CCDirector::sharedDirector()->getOpenGLView();
CCSize frameSize = pEGLView->getFrameSize();
pEGLView->setFrameSize(frameSize.height,frameSize.width);
pEGLView->setDesignResolutionSize(480,800, kResolutionExactFit);  //480,800爲該遊戲的分辨率大小(寬高)
#endif繼承

//切換橫屏代碼遊戲

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo minfo;
if( JniHelper::getStaticMethodInfo(minfo,"org/cocos2dx/zylgame/CzmjGame","changedActivityOrientation","(I)V") )
{
minfo.env->CallStaticVoidMethod(minfo.classID,minfo.methodID,2);
}
CCEGLView *pEGLView = CCDirector::sharedDirector()->getOpenGLView();
CCSize frameSize = pEGLView->getFrameSize();
pEGLView->setFrameSize(frameSize.height,frameSize.width);
pEGLView->setDesignResolutionSize(800,480, kResolutionExactFit);//480,800爲該遊戲的分辨率大小(寬高)
#endif開發

IOS解決方案:get

1.在IOS工程目錄下面找到RootViewController.h,RootViewController.mm拷貝一份命名爲RootViewControllerV.h,RootViewControllerV.mm,打開.mm文件修改如下代碼it

- (NSUInteger) supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
    return UIInterfaceOrientationMaskPortrait;//豎屏

//return UIInterfaceOrientationMaskLandscape;//橫屏
#endif
}

2.打開AppController.h增長如下代碼

@class RootViewControllerV;//類聲明

RootViewControllerV   *viewControllerV;//聲明實例變量

+(void)changeRootViewControllerH;//靜態方法(修改屏幕爲橫屏)
+(void)changeRootViewControllerV;//靜態方法(修改屏幕爲豎屏)

3.打開AppController.mm增長如下代碼

static AppController *s_self;

//修改函數didFinishLaunchingWithOptions增長如下代碼

    s_self = self;

    viewControllerV = [[RootViewControllerV alloc] initWithNibName:nil bundle:nil];
    viewControllerV.wantsFullScreenLayout = YES;

//橫屏切換靜態方法的實現
+(void)changeRootViewControllerH{
    EAGLView *__glView = (EAGLView *)s_self->viewControllerV.view;
    s_self->viewControllerV.view = nil;
    s_self->viewController.view = __glView;
   
    if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        [s_self->window addSubview:s_self->viewController.view];
    }
    [s_self->window setRootViewController:s_self->viewController];
   
    //[__glView setOriginalRect:__glView.frame];

//豎屏切換靜態方法的實現
+(void)changeRootViewControllerV{
    EAGLView *__glView = (EAGLView *)s_self->viewController.view;
    s_self->viewController.view = nil;
    s_self->viewControllerV.view = __glView;
   
    if ([[UIDevice currentDevice].systemVersion floatValue] < 6.0)
    {
        [s_self->window addSubview:s_self->viewControllerV.view];
    }
    [s_self->window setRootViewController:s_self->viewControllerV];
}

4.在C++代碼中調用以上兩個靜態方法來進行屏幕橫豎屏切換(C++調用OC即.cpp改成.mm進行混編)

 

開發中碰見的問題:

部分手機進行橫豎屏切換正常,部分設備切換時崩潰

1.檢查AndroidManifest.xml文件中是否有android:targetSdkVersion="18" 選項,移去該選項從新打包測試。

(估計只要設備android系統與該選項指定的API版本相同安裝該應用纔不會崩潰,移除該選項後其它設備方可正常運行)

相關文章
相關標籤/搜索