如何实施GameCenter排行榜?

时间:2015-01-24 22:26:52

标签: swift sprite-kit game-center game-center-leaderboard

我无法在Stack Overflow上关注其他教程和答案,那么如何在我的游戏中使用Swift中的SpriteKit实现GameCenter Leaderboards?一个人会使用什么代码,它会进入GameScene或GameViewController吗?我已经在iTunes Connect上设置了排行榜。我该怎么做?谢谢!

1 个答案:

答案 0 :(得分:7)

首先,您必须验证播放器:

 var localPlayer = GKLocalPlayer.localPlayer()
    localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in
        if ((viewController) != nil) {
            self.presentViewController(viewController, animated: true, completion: nil)
        }else{

            println((GKLocalPlayer.localPlayer().authenticated))
        }
    }

然后你可以打开排行榜:

 func showLeaderboard() {


        var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
        gcViewController.gameCenterDelegate = self

        gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards


        gcViewController.leaderboardIdentifier = "YOUR_BOARD_NAME"

        self.showViewController(gcViewController, sender: self)
        self.navigationController?.pushViewController(gcViewController, animated: true)

    }
    func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!)
    {

        self.dismissViewControllerAnimated(true, completion: nil)
    }

我建议在你的视图控制器中执行这些操作。在viewDidAppear上验证播放器,但不是viewDidLoad