向Game Center Xcode 6报告得分

时间:2014-09-11 02:19:20

标签: ios xcode swift game-center

我在苹果公司的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。有人可以告诉我如何将我的分数发布到游戏中心排行榜吗?谢谢!

2 个答案:

答案 0 :(得分:4)

我想出了怎么做。

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)
            }
        })


    }

答案 1 :(得分:4)

在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")
        }

    })
}