游戏中心身份验证仅适用于iOS 6的CCLayer Cocos2d

Kai*_*mer 1 iphone objective-c cocos2d-iphone ios ios6

我有一个似乎是一个相当普遍的问题,但我的搜索和解决方案的实现还没有解决.

我已经构建了一个仅用于景观的Cocos2d游戏,但需要访问Gamecenter.Gamecenter正在工作,启用了纵向模式,但它也允许游戏翻转到纵向模式.

我尝试了以下修复:

游戏中心仅在i OS 6中登录

仅横向应用程序中的GameCenter身份验证会引发UIApplicationInvalidInterfaceOrientation

将GameCenter添加到仅横向cocos2d应用程序后,iOS 6出错

Cocos 2d 2.0应该不能正常工作吗?

我认为问题在于我使用CCLayers而不是UIViewControllers构建了游戏

示例:MenuLayer.h

@interface MenuLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate, UINavigationControllerDelegate>{
   ..my header info..
}
Run Code Online (Sandbox Code Playgroud)

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.
                }
            }];
        }
    }

}
...
Run Code Online (Sandbox Code Playgroud)

由于我使用CCLayers而不是UIViewControllers构建了游戏,我还有哪些替代方案?假设CCLayers没有调用useInterfaceOrientations或者shouldAutorotate,我是否正确?

或者我应该以某种方式更改此代码来解决问题:

// Create a Navigation Controller with the Director
navController_ = [[UINavigationController alloc] initWithRootViewController:director_];
navController_.navigationBarHidden = YES;
Run Code Online (Sandbox Code Playgroud)

Syl*_*van 5

这让我感到沮丧了一段时间.在'Net上挖了一段时间之后,我找到了几个来源,有些使用iOS 6,有些使用iOS5,但我不得不进行一些修改,以便它在iOS5和iOS6上按照我想要的方式工作.这是我正在使用的代码,它使用5.1和6在我的iPhone上运行.请注意,Game Center登录仍然以纵向方式显示,似乎没有任何关于它的操作.但游戏的其余部分将保持横向模式.

  1. 在您的构建设置(info.plist)中启用纵向模式作为支持的方向.
  2. 创建UINavigationController的新子类.将这个类命名为对你有意义的.
  3. 在AppDelegate中,包含新的自定义UINavigationController头文件.
  4. 在您的App Delegate中,注释掉原始呼叫,然后呼叫您的自定义类.

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

#import <UIKit/UIKit.h>

@interface CustomNavigationViewController : UINavigationController

-(UIInterfaceOrientation) getCurrentOrientation;

@end
Run Code Online (Sandbox Code Playgroud)

和实施文件:

#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
Run Code Online (Sandbox Code Playgroud)

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

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

navController = [[CustomNavigationViewController alloc] initWithRootViewController:director];
window.rootViewController = navController;
navController.navigationBarHidden = YES;
[window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.