【问题标题】:Reporting score to Game Center Xcode 6向 Game Center Xcode 6 报告分数
【发布时间】:2014-09-11 02:19:19
【问题描述】:

我正在苹果的 Xcode 6 GM 种子上使用 swift 创建游戏。我正在添加游戏中心排行榜,并想向排行榜报告分数。除了报告分数功能,我已经完成了所有设置。这是我的代码:

func reportScores() {
    if GKLocalPlayer.localPlayer().authenticated == true{
        var highScore = userDefaults.integerForKey("myHighScore")
        var scoreReporter = GKScore(leaderboardIdentifier: "myLeaderboarID")
        scoreReporter.value = Int64(highScore)
        var scoreArray: [GKScore] = [scoreReporter]
        GKScore.reportScores([scoreReporter], withCompletionHandler: nil) {
        }

    }

}

我尝试了这个,然后出现一个错误,说 reportScore 方法不能转换为 $T2。有人可以告诉我如何将我的分数发布到 Game Center 排行榜吗?谢谢!

【问题讨论】:

    标签: ios xcode swift game-center


    【解决方案1】:

    我想出了办法。

    if GKLocalPlayer.localPlayer().authenticated == true{
            var highScore = userDefaults.integerForKey("highScore")
            var scoreReporter = GKScore(leaderboardIdentifier: "myLeaderboardID")
            scoreReporter.value = Int64(highScore)
            var scoreArray: [GKScore] = [scoreReporter]
            //println("report score \(scoreReporter)")
            GKScore.reportScores(scoreArray, {(error : NSError!) -> Void in
                if error != nil {
                    NSLog(error.localizedDescription)
                }
            })
    
    
        }
    

    【讨论】:

      【解决方案2】:

      在 Swift 2 中你可以这样做:

      func reportScores() {
          var gameScoreReporter = GKScore(leaderboardIdentifier: "yourLeaderboardID")
      
          gameScoreReporter.value = Int64(easyHighscore)
      
          var scoreArray: [GKScore] = [easyScoreReporter]
      
          GKScore.reportScores(scoreArray, withCompletionHandler: {(NSError) -> Void in
              if NSError != nil {
                  print(NSError!.localizedDescription)
              } else {
                  print("completed Easy")
              }
      
          })
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-01
        • 1970-01-01
        • 2012-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多