[cocos2d-x官方文档]设备方向

设备方向

这篇文章是描述怎么样在ios和android上设置设备的方向。

正如你所知道的,有两种方法来设置设备方向:

1. OpenGL / Cocos2d-x方式

这种方式很快,但是不会旋转UIKit对象(ios)或者小部件(android)。

2. ViewController方式(ios)或者设置xml(android)

这种方法有一点慢,但是UIKit对象(ios)或者小部件(android)就会正确的位置和方向。

openGL方式

你可以像下面那样设置设备方向:

CCDirector::sharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);

这种方式只能旋转cocos2d引擎渲染的对象,例如精灵。或者标签。

ViewController方式(ios)

让设备方向检测起作用可以像下面这样:

告诉系统那些方向是我们需要的支持(RootViewController.mm)

   
   
  1. // Override to allow orientations other than the default landscape orientation.
  2. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  3. return UIInterfaceOrientationIsLandscape( interfaceOrientation );
  4. }

把openGL渲染的view替换root view controller的view(AppController.mm)

   
   
  1. // Use RootViewController manage EAGLView
  2. viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
  3. viewController.wantsFullScreenLayout = YES;
  4. viewController.view = __glView;

这个代码是使用cocos2d-x实现的,可以作为ios的一个范例。

通过AndroidManifest.xml设置设备的方向

   
   
  1. <application android:label="@string/app_name" android:debuggable="true" android:icon="@drawable/icon">
  2. <activity android:name=".ApplicationDemo"
  3. android:label="@string/app_name"
  4. android:screenOrientation="landscape"
  5. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
  6. android:configChanges="orientation">
  7. <intent-filter>
  8. <action android:name="android.intent.action.MAIN" />
  9. <category android:name="android.intent.category.LAUNCHER" />
  10. </intent-filter>
  11. </activity>
  12. </application>

总结

1. 假如想要在ios中使用OpenGL的方式。那么就不要使用了RootViewController。

2. 假如想要在Android中使用OpenGL的方式,那么在AndroidManifest.xml中设置设备方向为竖直(portrait)。

3. 假如你不想要使用任何的UIkit的东西(ios)或者小部件(android),那么建议使用OpenGL的方式,这样最快。

支持多方向

以下设置多方向的方式是适用于cocos2d-x 2.0.2所创建的工作空间。

这种补丁可能不会在以后的cocos2d-x所支持,或者部分被弃用。

Ios

1.注释掉cocos2dx/platform/ios/CCEGLView.mm::setContentScaleFactor(…)中的assert(m_eResolutionPolicy kResolutionUnKnown);

2.注释掉cocos2dx/platform/CCEGLViewProtocol.cpp::setDesignResolutionSize(…)中的CCAssert(m_bIsRetinaEnabled false, “can not enable retina while set design resolution size!”);

3.在shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation的方法里面返回true。

4.在(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation中增加下面的代码:

   
   
  1. CGSize s;
  2. if (UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication.statusBarOrientation)) {
  3. s = CGSizeMake(std::max<float>(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height), std::min<float>(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height));
  4. } else {
  5. s = CGSizeMake(std::min<float>(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height), std::max<float>(UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height));
  6. }
  7.  
  8. cocos2d::CCDirector* director = cocos2d::CCDirector::sharedDirector();
  9. director->enableRetinaDisplay(false);
  10. director->getOpenGLView()->setFrameSize(s.width, s.height);
  11. director->getOpenGLView()->setDesignResolutionSize(s.width, s.height, kResolutionShowAll);
  12. director->enableRetinaDisplay(true);

Android

1. 把nativeInit(w, h);添加到cocos2dx/platform/android/java/src_common/org/cocos2dx/lib/Cocos2dxRenderer.java -> void onSurfaceChanged(GL10 gl, int w, int h).中。

2. 把下面的代码添加到void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h):

   
   
  1. cocos2d::CCEGLView* view = cocos2d::CCDirector::sharedDirector()->getOpenGLView();
  2. if (!view) {
  3. ...
  4. } else {
  5. ...
  6. if (view->getFrameSize().width != w || view->getFrameSize().height != h) {
  7. view->setFrameSize(w, h);
  8. view->setDesignResolutionSize(w, h, kResolutionShowAll);
  9. }
  10. }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值