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

在ios平台,cocos2dx的视图其实都是显示在一个UIViewController的。所以我们需要旋转UIViewController的方向。

在ios8以前只需调用以下代码:

复制代码
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    [UIViewController attemptRotationToDeviceOrientation];    

    s_self->viewController.view.transform = CGAffineTransformMakeRotation(M_PI/2);
    float width =s_self->viewController.view.bounds.size.width;
    float height =s_self->viewController.view.bounds.size.height;
    s_self->viewController.view.bounds = CGRectMake(0, 0, height, width);
复制代码

但是在ios8以后这样做就不行了。苹果不推荐手动修改UIViewController的方向。

苹果推荐的是重力使得屏幕自动旋转然后修改UI布局;或者是在切换UIViewController,新UIViewController有新的方向。

ios8做法:新建一个RootViewController2,代码跟RootViewController一样,只修改设置方向的相关代码。

在AppController里引入并初始化RootViewController2


但是在ios8以后这样做就不行了。苹果不推荐手动修改UIViewController的方向。

苹果推荐的是重力使得屏幕自动旋转然后修改UI布局;或者是在切换UIViewController,新UIViewController有新的方向。

ios8做法:新建一个RootViewController2,代码跟RootViewController一样,只修改设置方向的相关代码。

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

你可能感兴趣的:(cocos2d-x游戏开发屏幕横竖屏切换)