0

经过一些研究,这是我认为正确的代码。一切似乎都正常,没有错误。第一个上传的高分显示在排行榜上(对于我用于测试的两个 Game Center 帐户),但没有显示在初始高分(应该替换它)之后上传的任何分数(我尝试使用多个帐户,同样的问题)。这是我的代码(按下按钮时我调用此函数)。一切正常加载,游戏中心窗口完美关闭,没有错误,但帐户 1 的分数保持为 0(这是加载的第一个分数),而帐户 2 的分数保持在 2(这是加载的第一个分数)。(highScore 是调用此函数之前保存的 NSInteger)请帮忙!

 func saveHighscore() {

        //check if user is signed in
        if GKLocalPlayer.localPlayer().authenticated {


            let gameScoreReporter = GKScore(leaderboardIdentifier: "color_runner_leaderboard")

            gameScoreReporter.value = Int64(highScore)

            let scoreArray1: [GKScore] = [gameScoreReporter]

            GKScore.reportScores(scoreArray1, withCompletionHandler: {(NSError) -> Void in
                if NSError != nil {
                    print(NSError!.localizedDescription)
                } else {
                    print("completed Easy")
                }

            })

        }

    }

此代码显示排行榜:

 //show leaderboard screen
func showLeader() {
    let vc = self.view?.window?.rootViewController
    let gc = GKGameCenterViewController()
    gc.gameCenterDelegate = self
    vc?.presentViewController(gc, animated: true, completion: nil)
}

此代码用于上传分数(在加载 FIRST 分数后似乎不起作用)并显示排行榜

let highScoreDefault = NSUserDefaults.standardUserDefaults()
    if highScoreDefault.valueForKey("HighScoreSaved") != nil {
        highScore = highScoreDefault.valueForKey("HighScoreSaved") as! NSInteger!
        [saveHighscore()]
    }




    [showLeader()]
4

1 回答 1

0

尝试这个:

 func saveHighScore(identifier: String, score: Int) {

    if (GKLocalPlayer.localPlayer().authenticated) {

        let scoreReporter = GKScore(leaderboardIdentifier: identifier)
        scoreReporter.value = Int64(score)

        let scoreArray:[GKScore] = [scoreReporter]

        GKScore.reportScores(scoreArray, withCompletionHandler: {

            error -> Void in

            if (error != nil) {

                print("error")
            }
            else {
              print("Posted score of \(score)")
            }
        })
      }
    }

当您想保存高分时,添加以下代码:

self.saveHighScore("yourLeaderboardName", score: GameState.sharedInstance.highScore)

添加这个类。GameState.swift:

import Foundation

class GameState {
  var score: Int
  var highScore: Int
  var coins: Int

  init() {
    // Init
    score = 0
    highScore = 0
    coins = 0

    // Load game state
    let defaults = NSUserDefaults.standardUserDefaults()

    highScore = defaults.integerForKey("highScore")
    coins = defaults.integerForKey("coins")
  }

  func saveState() {
    // Update highScore if the current score is greater
    highScore = max(score, highScore)

    // Store in user defaults
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setInteger(highScore, forKey: "highScore")
    defaults.setInteger(coins, forKey: "coins")
    NSUserDefaults.standardUserDefaults().synchronize()
  }

  class var sharedInstance: GameState {
    struct Singleton {
        static let instance = GameState()
    }

    return Singleton.instance
  }
}

并将这个助手类添加到它自己的 Swift 文件中。不确定是否需要。我称之为 GameKitHelper.swift:

import UIKit
import Foundation
import GameKit

let PresentAuthenticationViewController = "PresentAuthenticationViewController"

class GameKitHelper: NSObject {

static let sharedInstance = GameKitHelper()


var authenticationViewController: UIViewController?
var gameCenterEnabled = false

func authenticateLocalPlayer() {

    //1
    let localPlayer = GKLocalPlayer()
    localPlayer.authenticateHandler = {(viewController, error) in

        if viewController != nil {
            //2
            self.authenticationViewController = viewController

                    NSNotificationCenter.defaultCenter().postNotificationName(PresentAuthenticationViewController, object: self)
        } else if error == nil {
            //3
            self.gameCenterEnabled = true
        }
      }

  }

  func reportAchievements(achievements: [GKAchievement], errorHandler: ((NSError?)->Void)? = nil) {
    guard gameCenterEnabled else {
        return
    }

    GKAchievement.reportAchievements(achievements, withCompletionHandler: errorHandler)
  }

    func showGKGameCenterViewController(viewController: UIViewController) {
      guard gameCenterEnabled else {
        return
    }

    //1
    let gameCenterViewController = GKGameCenterViewController()

    //2
    gameCenterViewController.gameCenterDelegate = self

    //3
    viewController.presentViewController(gameCenterViewController, animated: true, completion: nil)
 }

    func saveHighScore(identifier: String, score: Int) {

      if (GKLocalPlayer.localPlayer().authenticated) {

        let scoreReporter = GKScore(leaderboardIdentifier: identifier)
        scoreReporter.value = Int64(score)

        let scoreArray:[GKScore] = [scoreReporter]

        GKScore.reportScores(scoreArray, withCompletionHandler: {

            error -> Void in

            if (error != nil) {

                print("error")
            }
            else {

                print("Posted score of \(score)")
            }
        })
      }
     }
   }

extension GameKitHelper: GKGameCenterControllerDelegate {
  func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
  }
}
于 2015-12-23T04:11:10.387 回答