============================IOS========================
项目设置DeploymentInfo ->Device Orientation都不勾选
AppController.mm
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
RootViewController.h
+(void)changeDir:(bool)bIsLeft;
RootViewController.mm
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape( interfaceOrientation );
}
// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
- (NSUInteger) supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
return UIInterfaceOrientationMaskAllButUpsideDown;
#endif
}
static bool s_needOrientation=false;
+(void)changeDir:(bool)bIsLeft{
if(s_needOrientation == bIsLeft )
return;
s_needOrientation = bIsLeft ;
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:
bIsLeft ? (id)UIDeviceOrientationLandscapeLeft : (id)UIDeviceOrientationPortrait];
}
- (BOOL) shouldAutorotate {
//bool bIsPortraitCur = ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft);
bool bIsLeftCur=([UIDevice currentDevice].orientation==UIDeviceOrientationPortrait);
//CCLOG("s_needOrientation:%d",s_needOrientation);
//CCLOG("bIsPortraitCur:%d",bIsLeftCur);
if( bIsLeftCur != s_needOrientation )
return YES;
else
return NO;
}
//这是replaceScene时候的页面(先调用changeDirection,后转向需要转屏的页面)
h文件
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "RootViewController.h"
#endif
//声明转屏方法
void changeDirection();
cpp文件,需要把cpp文件后缀改成mm
竖屏
void Scene2::changeDirection(){
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
[RootViewController changeDir:true];
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
cocos2d::Size frameSize=glview->getFrameSize();
glview->setFrameSize(frameSize.height,frameSize.width);
director->getOpenGLView()->setDesignResolutionSize(1136.0f, 640.0f, ResolutionPolicy::EXACT_FIT);
#endif
}
横屏
void Scene1::changeDirection(){
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
[RootViewController changeDir:false];
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
cocos2d::Size frameSize=glview->getFrameSize();
glview->setFrameSize(frameSize.height,frameSize.width);
director->getOpenGLView()->setDesignResolutionSize(640.0f,1136.0f, ResolutionPolicy::EXACT_FIT);
#endif
}
=======================================================
=======================android=======================
1.
修改目录下的:proj.android\src\org\cocos2dx\lib\Cocos2dxActivity.java
添加下面3个 方法
//横屏
public static void ChangeLand(){
sContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
//竖屏
public static void ChangePort(){
sContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
//获得当前屏幕方向
public static int getDirection(){
//int strDirec=sContext.getRequestedOrientation();
//Log.e("direction",new String(strDirec+""));
//System.out.println(strDirec);
return sContext.getRequestedOrientation();
}
2.
//这是replaceScene时候的页面(先调用changeDirection,后转向需要转屏的页面)
h文件添加
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include <Jni.h>
#include "platform/android/jni/JniHelper.h"
#endif
//声明转屏方法
void changeDirection();
cpp文件添加
//竖屏转横屏
extern "C" //因为jni将java代码转过来是c的,所以C++引用得加上
{
void ChangeLand();
}
void Scene2::changeDirection(){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//判断当前设备的方向
//如果是横向就不做操作,竖向就强制横屏
JniMethodInfo info;
if(JniHelper::getStaticMethodInfo(info,"org/cocos2dx/lib/Cocos2dxActivity","getDirection","()I")){
jint iret=info.env->CallStaticIntMethod(info.classID,info.methodID);
if(iret==1){
if(JniHelper::getStaticMethodInfo(info,"org/cocos2dx/lib/Cocos2dxActivity","ChangeLand","()V"))
{
info.env->CallStaticVoidMethod(info.classID,info.methodID);
}
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
Size frameSize=glview->getFrameSize();
glview->setFrameSize(frameSize.height,frameSize.width);
director->getOpenGLView()->setDesignResolutionSize(1136.0f, 640.0f, ResolutionPolicy::EXACT_FIT);
}
}
#endif
}
//横屏转竖屏
extern "C" //因为jni将java代码转过来是c的,所以C++引用得加上
{
void ChangePort();
}
void Scene1::changeDirection(){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//判断当前设备的方向
//如果是竖向就不做操作,横向就强制竖屏
JniMethodInfo info;
if(JniHelper::getStaticMethodInfo(info,"org/cocos2dx/lib/Cocos2dxActivity","getDirection","()I")){
jint iret=info.env->CallStaticIntMethod(info.classID,info.methodID);
if(iret==0){
if(JniHelper::getStaticMethodInfo(info,"org/cocos2dx/lib/Cocos2dxActivity","ChangePort","()V"))
{
info.env->CallStaticVoidMethod(info.classID,info.methodID);
}
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
Size frameSize=glview->getFrameSize();
glview->setFrameSize(frameSize.height,frameSize.width);
glview->setDesignResolutionSize(640.0f, 1136.0f, ResolutionPolicy::EXACT_FIT);
}
}
#endif
}
3.
如在windows下调试
//横屏
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
glview->setFrameSize(656.0f,370.0f);//自定义的大小,1136*640的话太大
director->getOpenGLView()->setDesignResolutionSize(1136.0f, 640.0f, ResolutionPolicy::EXACT_FIT);
#endif
//竖屏
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
glview->setFrameSize(370.0f,656.0f);
//Size frameSize=glview->getFrameSize();
glview->setDesignResolutionSize(640.0f,1136.0f, ResolutionPolicy::EXACT_FIT);
#endif
最后附上AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.cocos.CocosProject"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto">
<uses-sdk android:minSdkVersion="9" android:maxSdkVersion="25" />
<uses-feature android:glEsVersion="0x00020000" />
<application android:label="@string/app_name"
android:icon="@drawable/icon">
<!-- Tell Cocos2dxActivity the name of our .so -->
<meta-data android:name="android.app.lib_name"
android:value="cocos2dcpp" />
<activity android:name="org.cocos2dx.cpp.AppActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<supports-screens android:anyDensity="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>