Swift显示游戏中心排行榜

时间:2015-01-02 22:25:38

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

现在我正试图让我创建的排行榜显示出来。玩家的身份验证很好,但是当游戏中心窗口打开时,这很奇怪。这是一张图片:

enter image description here

以下是我用来显示此图片的代码:

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.showLeaderboard()
    }
    func showLeaderboard() {
        var leaderView = UIViewController()

       var leaderViewController = GKGameCenterViewController(rootViewController: leaderView)
         leaderViewController.viewState = GKGameCenterViewControllerState.Leaderboards
       leaderViewController.leaderboardIdentifier = "High_Score_Board"
       self.showViewController(leaderViewController, sender: self)
        //self.presentViewController(leaderViewController, animated: true, completion: nil)

    }
    func leaderboardViewControllerDidFinish(controller: GKGameCenterViewController){
        controller.dismissViewControllerAnimated(true, completion: nil)
    }

所有这些都在我的GameViewController中。此外,即使这样可行,我如何在SKScenes中访问此方法?谢谢你的帮助!

1 个答案:

答案 0 :(得分:5)

导入GameKit:

import GameKit

请务必在班级中添加GKGameCenterControllerDelegate代表。

class ViewController: UIViewController, GKGameCenterControllerDelegate {
    ...
}

该代表需要一种方法,当玩家点击" Done"按钮。

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

这是包含显示排行榜所需代码的功能:

func showLeaderboard() {        
    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"
    self.showViewController(gcViewController, sender: self)
    self.navigationController?.pushViewController(gcViewController, animated: true)
     // self.presentViewController(gcViewController, animated: true, completion: nil)
}

现在可以通过按下按钮来呼叫showLeaderboard

@IBAction func buttonShowLeaderboard(sender: AnyObject) {
    showLeaderboard()
}