Gamecenter身份validation仅适用于iOS 6的CCLayer的Cocos2d

我有似乎是一个相当普遍的问题,但我的search和解决scheme的实施还没有解决。

我已经构build了一个Cocos2d游戏,只是为了风景,但需要访问Gamecenter。 Gamecenter正在工作,纵向模式启用,但它也允许游戏翻转到肖像模式。

我已经尝试了以下修复:

游戏中心loginlocking景观只在OS 6

只有横向应用程序中的GameCenter身份validation会引发UIApplicationInvalidInterfaceOrientation

将GameCenter添加到横向专用的cocos2d应用程序后,iOS 6中出现错误

科科斯2d 2.0 shouldAutorotate不工作?

我相信问题在于我使用CCLayers而不是UIViewControllers来构build游戏

例如:MenuLayer.h

@interface MenuLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate, UINavigationControllerDelegate>{ ..my header info.. } 

MenuLayer.m

 ... -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } -(BOOL)shouldAutorotate { return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait; } -(void)authenticateLocalPlayer { GKLocalPlayer * localPlayer= [GKLocalPlayer localPlayer]; if(localPlayer.authenticated == NO) { NSString *reqSysVer = @"6.0"; NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) { [[GKLocalPlayer localPlayer] setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) { if (viewcontroller != nil) { AppController *app = (AppController*) [[UIApplication sharedApplication] delegate]; [[app navController] presentModalViewController:viewcontroller animated:YES]; }else if ([GKLocalPlayer localPlayer].authenticated) { //do some stuff } })]; } else { [localPlayer authenticateWithCompletionHandler:^(NSError *error){ if(localPlayer.isAuthenticated) { //Peform Additionl Tasks for the authenticated player. } }]; } } } ... 

由于我已经使用CCLayers而不是UIViewControllers构build了游戏,我还有什么替代方法? 我是否正确地认为CCLayers不会调用使用supportedInterfaceOrientations或应该执行操作?

或者我应该改变这个代码以某种方式解决这个问题:

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

这让我感到沮丧了一段时间。 在“networking”上search了一段时间后,我发现了一些源代码,一些使用了iOS 6,一些使用了iOS 5,但是我不得不做一些修改,以便在iOS5和iOS6上以我想要的方式工作。 这是我使用的代码,它使用5.1和6.我的iPhone上工作。请注意,游戏中心login仍然以纵向方向出现,似乎没有任何可以做的。 但是游戏的其余部分将保持横向模式。

  1. 在构build设置(info.plist)中启用肖像模式作为支持的方向。
  2. 创build一个新的UINavigationController的子类。 无论对你有意义,命名这个类。
  3. 在你的AppDelegate中,包含你的新的自定义UINavigationController头文件。
  4. 在您的应用程序委托中,将原始调用注释掉,然后调用您的自定义类。

这应该够了吧。 这里是我的自定义类的代码:

 #import <UIKit/UIKit.h> @interface CustomNavigationViewController : UINavigationController -(UIInterfaceOrientation) getCurrentOrientation; @end 

并执行文件:

 #import "CustomNavigationViewController.h" @interface CustomNavigationViewController () @end @implementation CustomNavigationViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } // This is required to allow GameCenter to login in portrait mode, but only allow landscape mode for the rest of the game play/ // Arrrgg! -(BOOL) shouldAutorotate { return YES; } -(NSUInteger) supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } -(UIInterfaceOrientation) preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeRight; // or left if you prefer } -(NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) return UIInterfaceOrientationMaskLandscape; else { return UIInterfaceOrientationMaskAllButUpsideDown; } } -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait; } -(UIInterfaceOrientation) getCurrentOrientation { return [[UIDevice currentDevice] orientation]; } @end 

请注意,最后一个方法getCurrentOrientation不是必需的我只是把它放在那里,以防我想确定当前的方向是什么。

自定义类在AppDelegate.m中被调用,如下所示:(注释掉原始代码)

 navController = [[CustomNavigationViewController alloc] initWithRootViewController:director]; window.rootViewController = navController; navController.navigationBarHidden = YES; [window makeKeyAndVisible]; 

希望这可以帮助。