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版本相同安装该应用才不会崩溃,移除该选项后其它设备方可正常运行)

相关文章
相关标签/搜索