4

我无法在 Stack Overflow 上关注其他教程和答案,那么如何在我的游戏中,在 Swift 的 SpriteKit 中实现 GameCenter 排行榜?将使用什么代码,它会放在 GameScene 还是 GameViewController 中?我已经在 iTunes Connect 上设置了排行榜。我该怎么办?谢谢!

4

1 回答 1

8

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

 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

于 2015-01-24T22:58:40.190 回答