【问题标题】:Cocos 2d 2.0 shouldAutorotate not working?Cocos 2d 2.0 shouldAutorotate 不工作?
【发布时间】:2012-12-05 08:07:58
【问题描述】:
我遇到了一些问题。我有一个即将完成开发的 cocos2d 游戏。但是,我遇到了一个问题,我需要在我的应用程序 plist 中启用纵向方向,以便游戏中心登录工作而不会引发 SIGABRT 错误。因此,一旦我从我的应用程序的构建摘要页面启用它(或将其添加到 info.plist 文件作为支持的方向),它就可以正常工作。但是,在我的游戏中任何时候,如果你转动 iPhone,如果它感应到你这样转动它,它就会翻转到纵向模式。我已经尝试从我的 AppDelegate.m 中弄乱 shouldAutorotateToInterfaceOrientation 方法,但它根本没有被调用,也没有在任何时候被调用。我在方法中抛出了一个 NSLog 语句来确定它是否被调用,它不是。
所以,基本上我真正的问题是。当 Game Center 登录屏幕弹出时,我需要我的游戏保持横向模式。如何在 Cocos2d 2.0 游戏中做到这一点?
我正在使用 iOS6
【问题讨论】:
标签:
iphone
cocos2d-iphone
cocos2d-x
【解决方案1】:
首先,确保应用在目标摘要中支持纵向和横向。
然后你需要创建一个新的根视图控制器来强制你进入横向模式,这样你的游戏就不会开始奇怪地旋转:
@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;
现在您应该可以在顶部叠加纵向视图了。
希望这会有所帮助!
【解决方案2】:
在 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
【解决方案3】:
在 IOs6 中 shouldAutorotateToInterfaceOrientation 方法不起作用,所以你在 appDelegate .m 文件 [window addSubview:viewcontroller] 到 [window setRootviewcontroller:viewcontroller] 工作正常后更改。