问题描述
我有一个似乎是一个相当普遍的问题,但我的搜索和解决方案的实现没有解决。
我制作了一个Cocos2d游戏,仅用于风景,但需要访问Gamecenter。
我尝试了以下修正:
我相信问题是我使用CCLayers而不是UIViewControllers构建游戏
示例:
MenuLayer.h
@interface MenuLayer: CCLayer< GKAchievementViewControllerDelegate,GKLeaderboardViewControllerDelegate,UINavigationControllerDelegate> {
..my标题信息...
}
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)
{
//做一些东西
}
})];
}
else
{
[localPlayer authenticateWithCompletionHandler:^(NSError * error){
if(localPlayer.isAuthenticated)
{
/ / Peform Additionl认证播放器的任务。
}
}];
}
}
}
...
由于我使用CCLayers而不是UIViewControllers构建游戏,我有什么替代方法?我是否正确的假设CCLayers不调用使用supportedInterfaceOrientations或shouldAutorotate?
或者我应该改变这个代码以某种方式来解决问题:
//使用Director创建导航控制器
navController_ = [[UINavigationController alloc] initWithRootViewController:director_];
navController_.navigationBarHidden = YES;
在网上挖了一段时间后,我发现了一些来源,一些工作与iOS 6,一些与iOS5,但我不得不进行一些修改,以便它的工作方式我想在iOS5和iOS6。这是我使用的代码,它在我的iPhone上使用5.1和6.注意,游戏中心登录仍然出现在纵向,没有看起来有什么你可以做的。
- 在构建设置中启用纵向模式作为支持的方向(info.plist) 。
- 创建UINavigationController的新子类。
- 在您的AppDelegate中,包含您的新的自定义UINavigationController头文件。
- 在您的应用程序代理中,注释掉
这样做就可以了。这里是我的自定义类的代码:
#import< UIKit / UIKit.h>
@interface CustomNavigationViewController:UINavigationController
- (UIInterfaceOrientation)getCurrentOrientation;
@end
而实施文件:
#importCustomNavigationViewController.h
@interface CustomNavigationViewController()
@end
@implementation CustomNavigationViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName: nibNameOrNil bundle:nibBundleOrNil];
if(self){
//自定义初始化
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//在加载视图之后执行任何其他设置。
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//处理可以重新创建的任何资源。
}
//这是需要允许GameCenter以纵向模式登录,但是只允许横向模式在游戏剩余时间播放/
// Arrrgg!
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight; //或者如果你喜欢
就可以离开}
- (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];
希望这有帮助。
I'm having what seems to be a fairly common problem, but my searches and implementations of solutions have not worked out.
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've attempted the following fixes:
Game center login lock in landscape only in i OS 6
GameCenter authentication in landscape-only app throws UIApplicationInvalidInterfaceOrientation
Error in iOS 6 after adding GameCenter to a landscape-only cocos2d app
Cocos 2d 2.0 shouldAutorotate not working?
I believe the problem is that I've built the game using CCLayers instead of UIViewControllers
Example: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.
}
}];
}
}
}
...
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;
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.
- enable portrait mode as a supported orientation in your build settings (info.plist).
- Create a new subclass of UINavigationController. Name this class whatever makes sense to you.
- In your AppDelegate, include your new custom UINavigationController header file.
- In your App Delegate, comment out the original call and instead call your custom class.
That should do the trick. Here is the code from my custom class:
#import <UIKit/UIKit.h>
@interface CustomNavigationViewController : UINavigationController
-(UIInterfaceOrientation) getCurrentOrientation;
@end
And the implementation file:
#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
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.
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];
Hope this helps.
这篇关于Gamecenter认证在风景只有Cocos2d与CCLayer iOS 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!