-4

我使用下面的代码在我的应用程序中创建了我的 GameCenter 页面。但是,我无法将在 iTunes Connect 上创建的排行榜连接到我的代码,因此该应用程序只会生成一个空白的排行榜页面。如何将我的 Itunes Connect 排行榜连接到我的代码,以及如何制作应用程序,以便将您的分数放在排行榜上,因为现在排行榜是空的。

这是我使用的代码:

override func viewDidAppear(animated: Bool) {

    //check user is logged into GameCenter
    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))
        }

    }

//display leaderboard
func showLeaderboard() {

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

    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards

    gcViewController.leaderboardIdentifier = "MyLeaderboard"

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

}

//take leaderboard away
func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!) {

    self.dismissViewControllerAnimated(true, completion: nil)

}

如何将我的 Itunes Connect 排行榜连接到应用程序,以及使用什么代码将分数上传到排行榜?

4

1 回答 1

1

首先添加GKGameCenterControllerDelegate到你的类:

class viewController: UIViewController, GKGameCenterControllerDelegate {
...
}

这是您需要用来验证播放器的代码:

func login() {
    println("Game Center Login Called")
    let localPlayer = GKLocalPlayer.localPlayer()

    // Handle the authentication
    localPlayer.authenticateHandler = {(Home: UIViewController!, error: NSError!) -> Void in
        if Home != nil {
            println("Authentication is being processed.")
            self.presentViewController(Home, animated: true, completion: nil)

        } else {
            println("Player has been successfully authenticated.")
        }
    }
}

这是您应该用来显示排行榜的代码:

func showLeaderboard() {

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

    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards

    gcViewController.leaderboardIdentifier = "YOUR_LEADERBOARD_ID"
    self.showViewController(gcViewController, sender: self)
    self.presentViewController(gcViewController, animated: true, completion: nil)
}

当用户点击“完成”时需要此代码。

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

可以在方法login()中调用认证viewDidLoad方法:

override func viewDidLoad() {
    super.viewDidLoad() 
login()
...
}

当用户点击按钮时显示排行榜

@IBAction func button(sender: AnyObject) {

    showLeaderboard()
}

如果您想提交最好的成绩:

if GKLocalPlayer.localPlayer().authenticated {
                        println("I have submitted the score to Game Center")
                        let gkScore = GKScore(leaderboardIdentifier: "Best_Score")
                        gkScore.value = Int64(bestScore)
                        GKScore.reportScores([gkScore], withCompletionHandler: ( { (error: NSError!) -> Void in
                            if (error != nil) {
                                // handle error
                                println("Error: " + error.localizedDescription);
                            } else {
                                println("Score reported: \(gkScore.value)")
                            }
                        }))
                    }
于 2015-01-25T08:33:56.760 回答