Cocos 2d 2.0 应该自动旋转不起作用



我遇到了一些问题。我有一个cocos2d游戏,我刚刚完成开发。但是,我遇到了一个问题,我需要在我的应用程序 plist 中启用纵向,以便游戏中心登录才能正常工作而不会引发 SIGABRT 错误。因此,一旦我从应用程序的构建摘要页面启用它(或将其作为支持的方向添加到 info.plist 文件中),它就可以正常工作。但是在我的游戏中,如果你把iPhone调过来,它会翻到纵向模式,如果它感觉到你这样转动它。我尝试从我的AppDelegate.m弄乱shouldAutorotateToInterfaceOrientation方法,它根本没有被调用,任何时候都没有被调用。我在方法中抛出了一个 NSLog 语句,以确定它是否被调用,而不是被调用。

所以,基本上我真正的问题是。我需要我的游戏保持横向模式,除了当游戏中心登录屏幕弹出时。如何在 Cocos2d 2.0 游戏中执行此操作?

我正在使用 iOS6

首先,确保应用在目标摘要中支持纵向和横向。

然后,您需要创建一个新的根视图控制器,强制您进入横向模式,这样您的游戏就不会开始奇怪地旋转:

@implementation CUSTOM_RootViewController
-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate {
    return YES;
}
@end

最后,在 AppDelegate.m 文件中,将原始导航控制器替换为新导航控制器:

// Create a Navigation Controller with the Director
//navController_ = [[UINavigationController alloc] initWithRootViewController:director_];
navController_ = [[SMD_RootViewController alloc] initWithRootViewController:director_];
navController_.navigationBarHidden = YES;

现在,您应该能够在顶部叠加纵向视图。

希望这有帮助!

AppDelegate.mm 中使用此代码

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_6_0
-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
    return UIInterfaceOrientationMaskLandscape;
}
#else
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
#endif

在 IOs6 中应该自动旋转到接口方向方法不起作用,所以你将 appDelegate .m 文件 [window addSubview:viewcontroller] 更改为 [window setRootviewcontroller:viewcontroller] 后它工作正常。

最新更新