Game Center与Sprite Kit集成?

时间:2013-10-20 03:50:21

标签: ios ios7 game-center sprite-kit

如何使用Sprite Kit Xcode模板使用Game Center或GameKit Framework? 在Sprite工具包中,它使用场景;但通常要查看排行榜,例如你需要“presentModalViewController”,但这在SKView中是不可能的。

我如何在iOS 6中验证播放器以及所有其他有趣的内容。

提前致谢!

4 个答案:

答案 0 :(得分:5)

您可以使用此代码访问根视图控制器

来使用“presentModalViewController”
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: gameCenterController animated: YES completion:nil];

现在,您可以访问任何包含在SKScenes中的ModelViewController。我在最新的游戏中做到了,效果很好

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

答案 1 :(得分:0)

这是一个更新的身份验证本地播放器,但Ravindra的代码也有效。

- (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];
        }
    };
}

答案 2 :(得分:0)

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

启用游戏中心的场景中的调用方法:

中的HUD或GameScene
override func didMoveToView(view: SKView)` 
    authenticateLocalPlayer()

答案 3 :(得分:-1)

您可以像这样进行身份验证

[[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]);
            }
        }];
    }