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


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方法,如下所示

//切換豎屏代碼 

#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解決方案:

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

- (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版本相同安裝該應用才不會崩潰,移除該選項后其它設備方可正常運行)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM