尝试这个:
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)
}
}