1

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

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

我尝试了以下修复:

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

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

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

Cocos 2d 2.0 shouldAutorotate 不工作?

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

示例:MenuLayer.h

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

菜单层.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 是否正确?

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

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

1 回答 1

5

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

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

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

#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];

希望这可以帮助。

于 2013-01-10T23:33:53.617 回答