2016-03-14 94 views
0

我正在嘗試將遊戲中心功能添加到我的應用程序中,並且這個功能將用戶的高分添加到排行榜上。問題是它只上傳第一個高分。當用戶獲得新的高分時,我再次調用它,但它確實有效。快速遊戲中心保存高分功能不起作用

//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

你可以嘗試從這個替換完成處理該塊:

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

這樣:

GKScore.reportScores(scoreArray) {(error) in 
    self.lastError = error 
} 
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)