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
开发中遇见的问题:
部分手机进行横竖屏切换正常,部分设备切换时崩溃
1.检查AndroidManifest.xml文件中是否有android:targetSdkVersion="18" 选项,移去该选项重新打包测试。
(估计只要设备android系统与该选项指定的API版本相同安装该应用才不会崩溃,移除该选项后其它设备方可正常运行)