游戏中心与Sprite Kit整合?

我如何使用Game Center或GameKit框架与Sprite Kit Xcode模板? 在Sprite套件中,它使用场景; 但通常查看排行榜,例如你需要“presentModalViewController”,但在SKView中是不可能的。

我怎样才能在iOS 6中authentication玩家和所有其他有趣的东西。

提前致谢!

你可以像这样进行validation

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) { if (error == nil) { static_setEnable( true ); NSLog(@" Authenticate local player complete"); } else { static_setEnable( false ); NSLog(@"Authenticate local player Error: %@", [error description]); } }]; } 

您可以使用“presentModalViewController”通过使用此代码来访问根视图控制器

 UIViewController *vc = self.view.window.rootViewController; [vc presentViewController: gameCenterController animated: YES completion:nil]; 

现在,您可以访问SKScenes中的任何位置的ModelViewController。 我在最新的游戏中做到了,并且运行得很好

此外,我build议你使用单独的对象来控制游戏中心,比如排行榜和成就,这样你可以在下一场游戏中重复使用它。

这里是一个更新的身份validation本地球员,但拉文德拉的代码也可以。

 - (void) authenticateLocalPlayer { GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){ if (viewController != nil) { //showAuthenticationDialogWhenReasonable: is an example method name. Create your own method that displays an authentication view when appropriate for your app. //[self showAuthenticationDialogWhenReasonable: viewController]; } else if (localPlayer.isAuthenticated) { //authenticatedPlayer: is an example method name. Create your own method that is called after the loacal player is authenticated. //[self authenticatedPlayer: localPlayer]; } else { //[self disableGameCenter]; } }; } 

Swift 2.0

  func authenticateLocalPlayer() { let localPlayer = GKLocalPlayer.localPlayer() localPlayer.authenticateHandler = { (viewController, error ) -> Void in if (viewController != nil) { let vc:UIViewController = self.view!.window!.rootViewController! vc.presentViewController(viewController!, animated: true, completion:nil) } else { print ("Authentication is \(GKLocalPlayer.localPlayer().authenticated) ") GlobalData.loggedIntoGC = true // do something based on the player being logged in. 

GlobalData Swift文件:

 static var loggedIntoGC:Bool = false 

在Game Center正在启用的场景中调用Method:例如HUD或GameScene

 override func didMoveToView(view: SKView)` authenticateLocalPlayer()