2013-01-10 149 views
1

我有似乎是一個相當普遍的問題,但我的解決方案的搜索和實現尚未解決。Gamecenter身份驗證僅適用於iOS 6的Cocos2d與CCLayer 6

我已經構建了一個Cocos2d遊戲,目的只是爲了風景,但需要訪問Gamecenter。 Gamecenter正在開展工作,並啓用了肖像模式,但它也允許遊戲轉到肖像模式。

我已經嘗試以下修正:

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?

我相信這個問題是我建立的使用CCLayers遊戲而不是UIViewControllers

例子: 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的比賽中,我有什麼辦法?我是否正確地認爲CCLayers不會調用supportedInterfaceOrientations或shouldAutorotate?

或者我應該以某種方式改變這個代碼來解決這個問題:

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

如果您的問題是關於Game Center登錄屏幕僅處於肖像模式,那麼您就必須忍受這種情況。登錄發生在肖像。我沒有聽說過,也沒有看到有人爲此做過工作。 – LearnCocos2D

回答

5

這我感到沮喪的一段時間了。在網絡上搜索了一段時間後,我發現了一些源代碼,一些使用iOS 6,一些使用iOS5,但我不得不做一些修改,以便在iOS5和iOS6上以我想要的方式工作。這是我使用的代碼,它使用5.1和6在我的iPhone上工作。請注意,Game Center登錄仍然以縱向方式顯示,似乎沒有任何可以做的事情。但遊戲的其餘部分將保持橫向模式。

  1. 啓用肖像模式作爲您的構建設置(info.plist)中的支持方向。
  2. 創建UINavigationController的新子類。無論對你有意義,命名這個類。
  3. 在您的AppDelegate中,包含您的新自定義UINavigationController頭文件。
  4. 在您的應用程序委託中,將原始調用註釋掉,然後調用您的自定義類。

這應該做的伎倆。這裏是我的自定義類的代碼:我想確定是什麼

#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不需要我只是把在那裏的情況:

#import <UIKit/UIKit.h> 

@interface CustomNavigationViewController : UINavigationController 

-(UIInterfaceOrientation) getCurrentOrientation; 

@end 

和實現文件目前的方向是。

自定義類被稱爲在AppDelegate.m這樣的:(註釋掉原碼)

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

希望這有助於。

+0

我希望我能再投票10次。先生,先生,打得好。 – ezekielDFM

+0

它正在工作...偉大的工作..但如果我們旋轉移動portraitupsidedown然後遊戲將旋轉。所以我們必須在shouldAutorotateToInterfaceOrientation中添加另一個或條件。雖然......尼斯爲我工作。 – Renaissance

+0

萬億感謝!!!!!!它也解決了iAD的景觀旋轉錯誤!!!!! – ColdSteel

相關問題