我正在尝试将游戏中心功能添加到我的应用程序中,这是我的功能,以获得用户的高分并放入排行榜。问题是它只上传了第一个高分。当用户获得新的高分但我确实有效时,我再次调用它。
//send high score to leaderboard
func saveHighscore(score:Int)
{
//check if user is signed in
if GKLocalPlayer.localPlayer().authenticated
{
let scoreReporter = GKScore(leaderboardIdentifier: "774433bbcc11bbvv") //leaderboard id here
scoreReporter.value = Int64(circleView1.score) //score variable here (same as above)
let scoreArray: [GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray,withCompletionHandler: {(error : NSError?) -> Void in
if error != nil
{
print("error")
}
})
}
}
答案 0 :(得分:0)
您可以尝试使用完成处理程序替换块:
GKScore.reportScores(scoreArray,withCompletionHandler: {(error : NSError?) -> Void in
if error != nil
{
print("error")
}
})
到此:
GKScore.reportScores(scoreArray) {(error) in
self.lastError = error
}
答案 1 :(得分:0)
游戏结束时,您是否使用过NSUserDefaults来保存和更新新的高分数。
***Game Scene***
var defaults = NSUserDefaults()
var score:Int = 0
var highScore:Int = 0
func saveHighScore(score:Int) {
//Check If User is Signed In
if GKLocalPlayer.localPlayer().authenticated {
let scoreReporter = GKScore(leaderboardIdentifier: "774433bbcc11bbvv")
scoreReporter.value = Int64(score)
let scoreArray: [GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray, withCompletionHandler: {(error : NSError?) -> Void in
if error != nil {
print("error")
}
})
}
}
游戏结束 //获得高分
let highScore = NSUserDefaults.standardUserDefaults().integerForKey("highScore")
if (score > highScore){
defaults.setInteger(score, forKey: "highScore")
}
游戏结束 //将分数保存到排行榜
saveHighScore(score)