【问题标题】:Changing Cocos 2d orientation landscape to portrait将 Cocos 2d 方向横向更改为纵向
【发布时间】:2012-01-27 13:27:07
【问题描述】:

我有两个视图,其中一个是横向的,另一个是纵向的。默认方向是横向。当我使用

将视图从横向更改为纵向时
[[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeRight];

虽然图形旋转完美,但触摸屏出现问题。谁能帮我解决这个问题?

【问题讨论】:

  • 这不是frame bounds定义的问题吗?
  • @Haroon:我觉得他是指活跃区域?
  • 好吧,我的意思是屏幕的触摸区域不会随着方向而改变。但是图形的位置是完美旋转的
  • @Kheldar 我不知道 cocos 2d 框架边界,因为我是 Cocos 2d 的新手。

标签: ios cocos2d-iphone orientation


【解决方案1】:

你使用的是最新的 cocos2d 模板吗? [[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeRight]; 不起作用的原因是因为默认情况下,它让 UIKit (RootViewController) 处理旋转,从第 77 - 84 行读取 cmets。您的代码所做的是更改 CCDirector 方向,而不是 RootViewController因此您的触摸位置没有正确翻译。

您是否将 Apple 标准 UIKit 与 cocos2d 混合使用?如果您没有一个非常简单的解决方案,那就去GameConfig.h

更改这些行

#if defined(__ARM_NEON__) || TARGET_IPHONE_SIMULATOR
#define GAME_AUTOROTATION kGameAutorotationUIViewController

#if defined(__ARM_NEON__) || TARGET_IPHONE_SIMULATOR
#define GAME_AUTOROTATION kGameAutorotationCCDirector

P.S 更改自动旋转处理程序可能/可能不起作用,具体取决于您设置项目的方式。

【讨论】:

    【解决方案2】:

    实现正确的方向非常简单明了。您只需要编辑您的 RootViewController 实现:只需修改它的 -willChangeToInterfaceOrientation... 方法,为您在应用程序中需要的方向返回“YES”。例如,将所有可能性的返回值更改为

    return ( (interfaceOrientation == UIInterfaceOrientationPortrait) ||    
    (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) );
    

    您的应用只有在横向到纵向时才会旋转。而且您的应用程序(在这种情况下)仅支持纵向。 要正确地自动旋转您的应用程序以响应从纵向更改为纵向UpsideDown(反之亦然),只需编辑以下行以使用适当的值配置您的 CCDirector 实例:

    if( interfaceOrientation == UIInterfaceOrientationPortrait ) {
        [[CCDirector sharedDirector] setDeviceOrientation:     
    kCCDeviceOrientationPortraitUpsideDown];
    } else if( interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [[CCDirector sharedDirector] setDeviceOrientation: 
    kCCDeviceOrientationPortrait];
    }
    

    cmets 中提到的所有内容都来自您的模板项目的默认方法实现!

    不要忘记编辑您应用的 info.plist 以包含键的适当值 -初始界面定位 -SupportedInterfaceOrientations

    您通常不需要更改应用委托类或 GameConfig.h 中的任何内容。

    然后......你去!

    【讨论】:

      【解决方案3】:

      这很简单,只需将触摸点转换为 GL 即可解决我的问题

      CGPoint convertedLocation = [[CCDirector sharedDirector]convertToGL:point];
      

      【讨论】:

        【解决方案4】:

        [startUpOptions setObject:CCScreenOrientationPortrait forKey:CCSetupScreenOrientation];

        我刚刚在函数 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 的 appdelegate.m 文件中添加了上面的代码,并且它起作用了

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-14
          • 2020-10-13
          • 1970-01-01
          • 2015-01-10
          • 2016-05-03
          • 2012-07-21
          • 2020-06-12
          • 1970-01-01
          相关资源
          最近更新 更多