带有 CCLayer for iOS 6 的 Cocos2d 横向 Gamecenter 身份验证

Gamecenter authentication in landscape only Cocos2d with CCLayer for iOS 6(带有 CCLayer for iOS 6 的 Cocos2d 横向 Gamecenter 身份验证)

本文介绍了带有 CCLayer for iOS 6 的 Cocos2d 横向 Gamecenter 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个似乎相当普遍的问题,但我对解决方案的搜索和实施都没有成功.

I'm having what seems to be a fairly common problem, but my searches and implementations of solutions have not worked out.

我已经构建了一个 Cocos2d 游戏,该游戏仅用于横向,但需要访问 Gamecenter.Gamecenter 正在运行,启用了纵向模式,但它也允许游戏切换到纵向模式.

I've built a Cocos2d game that is intended to be landscape only, but needs to access Gamecenter. Gamecenter is working, with portrait mode enabled, but it's also allowing the game to flip to portrait mode too.

我尝试了以下修复:

游戏中心登录锁定仅限横向在 i OS 6 中

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

iOS 中的错误6 将 GameCenter 添加到纯横向 cocos2d 应用后

Cocos 2d 2.0 shouldAutorotate 不工作?

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

I believe the problem is that I've built the game using CCLayers instead of UIViewControllers

示例:MenuLayer.h

Example: MenuLayer.h

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

MenuLayer.m

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 构建了游戏,我有什么替代方案?我假设 CCLayers 不调用 use supportedInterfaceOrientations 或 shouldAutorotate 是否正确?

Since I've built the game using CCLayers instead of UIViewControllers, what alternatives do I have? Am I correct in assuming that CCLayers don't call use supportedInterfaceOrientations or shouldAutorotate?

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

Or am I supposed be changing this code somehow to fix the problem:

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

推荐答案

这也让我很沮丧.在网上搜索了一段时间后,我发现了一些资源,其中一些使用 iOS 6,一些使用 iOS5,但我必须进行一些修改,以便它在 iOS5 和 iOS6 上都以我想要的方式工作.这是我正在使用的代码,它适用于我使用 5.1 和 6 的 iPhone.请注意,游戏中心登录仍然以纵向显示,您似乎对此无能为力.但游戏的其余部分将保持横向模式.

This frustrated me for awhile too. After digging around for awhile on the 'Net I found a couple of sources and some worked with iOS 6, some with iOS5, but I had to make some modifications so that it worked the way I wanted on both iOS5 and iOS6. This is the code I am using, it works on my iPhone using 5.1 and 6. Note that the Game Center login still comes up in portrait orientation, there doesn't appear to be anything you can do about that. But the rest of the game will remain in landscape mode.

  1. 在构建设置 (info.plist) 中启用纵向模式作为支持的方向.
  2. 创建一个新的 UINavigationController 子类.给这个类起任何对你有意义的名字.
  3. 在您的 AppDelegate 中,包含新的自定义 UINavigationController 头文件.
  4. 在您的 App Delegate 中,注释掉原来的调用,改为调用您的自定义类.

这应该可以解决问题.这是我的自定义类的代码:

That should do the trick. Here is the code from my custom class:

#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 不是必需的,我只是把它放在那里,以防我想确定当前方向是什么.

Note that last method getCurrentOrientation isn't required I just put that in there in case I wanted to determine what the current orientation is.

自定义类在 AppDelegate.m 中是这样调用的:(注释掉原来的代码)

The custom class is called in AppDelegate.m like this: (comment out the original code)

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

希望这会有所帮助.

这篇关于带有 CCLayer for iOS 6 的 Cocos2d 横向 Gamecenter 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有 CCLayer for iOS 6 的 Cocos2d 横向 Gamecenter 身份验证

基础教程推荐