1

如何使用游戏技术 Sprite Kit 将游戏中心排行榜和成就添加到 Xcode 6 中的 iOS 应用程序,编程语言设置为 swift?

4

1 回答 1

1

这是我的游戏中心代码。你将把它放在你的游戏场景代码中。您必须在 iTunes Connect 中创建排行榜,并且您的排行榜名称必须与您在代码中输入的一致。(我称之为“排行榜名称”)。在你的类 GKGameCenterControllerDelegate 的顶部添加这个委托也很重要

//MARK: GameCenter

//send high score to leaderboard
func saveHighscore(score:Int) {

    //check if user is signed in
    if GKLocalPlayer.localPlayer().authenticated {

        var scoreReporter = GKScore(leaderboardIdentifier: "leaderboardName") //leaderboard id here

        scoreReporter.value = Int64(score) //score variable here (same as above)

        var scoreArray: [GKScore] = [scoreReporter]

        GKScore.reportScores(scoreArray, {(error : NSError!) -> Void in
            if error != nil {
                println("error")
            }
        })

    }

}


//shows leaderboard screen
func showLeader() {
    var vc = self.view?.window?.rootViewController
    var gc = GKGameCenterViewController()
    gc.gameCenterDelegate = self
    vc?.presentViewController(gc, animated: true, completion: nil)
}

//hides leaderboard screen
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
{
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)

}

//initiate gamecenter
func authenticateLocalPlayer(){

    var localPlayer = GKLocalPlayer.localPlayer()

    localPlayer.authenticateHandler = {(viewController, error) -> Void in

        if (viewController != nil) {
            let vc: UIViewController = self.view!.window!.rootViewController!
            vc.presentViewController(viewController, animated: true, completion: nil)
        }

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

}
于 2015-04-05T23:08:02.310 回答