在SpriteKit中显示游戏中心排行榜

时间:2015-06-26 17:52:26

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

我正在开发游戏,当用户点击按钮时,我正在寻找帮助或代码以在我的应用中显示GameCenter排行榜。我不知道从哪里开始,所有其他答案似乎都是Obj-C,谢谢!

编辑: 下面的答案非常有效,但对于那些想知道如何在SpriteKit中执行此操作的人,只需将以下方法添加到GameViewController并添加通知中心观察器

NSNotificationCenter.defaultCenter().addObserver(self, selector: "showLeaderboard", name: "showLeaderboard", object: nil)

SKScene班级中,只需致电该观察员。

NSNotificationCenter.defaultCenter().postNotificationName("showLeaderboard", object: nil)

只是为了帮助那些疑惑的人!

2 个答案:

答案 0 :(得分:1)

你可以像CeceXX所展示的那样,或者你可以使用DaRkD0G的Easy-Game-Center来简化它。 https://github.com/DaRkD0G/Easy-Game-Center-Swift

答案 1 :(得分:0)

在您的班级中加入GKGameCenterControllerDelegate协议。

class ViewController: UIViewController, GKGameCenterControllerDelegate

此方法在点击“完成”时取消游戏中心视图:

func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

此功能包括显示排行榜所需的代码:

func showLeaderboard() {

    // declare the Game Center viewController
    var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
    gcViewController.gameCenterDelegate = self

    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards

    // Remember to replace "Best Score" with your Leaderboard ID (which you have created in iTunes Connect)
    gcViewController.leaderboardIdentifier = "Best_Score"
    // Finally present the Game Center ViewController
    self.showViewController(gcViewController, sender: self)
    self.navigationController?.pushViewController(gcViewController, animated: true)
    self.presentViewController(gcViewController, animated: true, completion: nil)
}

现在,您可以按showLeaderboard

来触发功能UIButton
@IBAction func buttonShowLeaderboard(sender: AnyObject) {
    showLeaderboard()
}