ゲームの起動時にユーザーのハイスコアを取得できるようにGKLeaderboard.loadScoresWithCompletionHandler
、プロパティを使用してきました。localPlayerScore
Game Center
現在 iOSでは、localPlayerScore プロパティにアクセスすると有効なオブジェクトが取得7.0
されます。ただし、 iOSでは nil を取得しているため、アプリがクラッシュします。オプションのアンラップ中に nil が見つかったという実行時エラーが発生します。8.1.2
GKScore
8.1.3
この問題に関連するコードのスニペットを次に示します。
func compareLocalHighScoreFromLeaderboards()
{
// This is to fetch data from the high score leaderboard
let leaderboardRequest = GKLeaderboard()
leaderboardRequest.identifier = GlobalVariables.sharedInstance._highScoreLeaderboardID
leaderboardRequest.loadScoresWithCompletionHandler { (scores, error) -> Void in
if error != nil
{
println("Error fetching score from leaderboards: \(error))")
}
else if scores != nil
{
println("entered loading scores from leaderboards")
let leaderboardScore = leaderboardRequest.localPlayerScore // this returns a GKScore object
var playerLocalHighScore = NSUserDefaults.standardUserDefaults().objectForKey(GlobalVariables.sharedInstance._highScoreKey) as NSNumber
// Check first if the saved highscore is updated.
if playerLocalHighScore.longLongValue != leaderboardScore.value
{
// this means that we don't have the updated leaderboard score in our device
if playerLocalHighScore.longLongValue > leaderboardScore.value
{
println("Local score is greater than leaderboard highscore. Do nothing because GameKit will automatically report to GC when there is internet connectivity")
}
else if playerLocalHighScore.longLongValue < leaderboardScore.value
{
// update the local highscore with the leaderboard highscore
let updatedHighscore: NSNumber = NSNumber(longLong: leaderboardScore.value)
NSUserDefaults.standardUserDefaults().setObject(updatedHighscore, forKey: GlobalVariables.sharedInstance._highScoreKey)
// send notification message to MainMenuScene to update the SKLabelNode
NSNotificationCenter.defaultCenter().postNotificationName(UpdatedLocalHighScore, object: nil)
}
}
else
{
println("The local highscore and the leaderboard highscore are already in sync")
}
}
}
}
ユーザーのゲームセンターのハイスコアを取得するために以前のゲーム (ちなみに承認され、Appstore に掲載されています) にこの同じコードを使用してきましたが、8.1.3
.
8.1.3
私の現在のアプリは、iOSデバイスでテストしたところクラッシュしたため、Apple に拒否されました。デバッグを行ったところ、これlocalPlayerScore
が nil であることが問題の原因であることがわかりました。
最近この問題に直面している人はいますか?この問題を解決する方法を知っている人はいますか?
どんな助けでも大歓迎です。