我最近尝试将Game Center添加到我的Sprite Kit游戏中,但它无法正常工作。 所以我将分数发送到GC排行榜,我没有收到任何错误,并且分数显着发送但我仍然看不到排行榜中列出的分数!排行榜本身列在游戏中心,没有得分。
GameViewController
import GameKit
class GameViewController: UIViewController, GKGameCenterControllerDelegate{
override func viewDidLoad() {
super.viewDidLoad()
let skView = self.view as! SKView
let scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFit
skView.presentScene(scene)
authenticateLocalPlayer()
}
//Initiate GameCenter
func authenticateLocalPlayer(){
var localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if (viewController != nil) {
self.presentViewController(viewController, animated: true, completion: nil)
}
else {
println((GKLocalPlayer.localPlayer().authenticated))
}
}
}
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
{
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
}
游戏状态
import Foundation
import GameKit
class GameState {
var score: Int
var highScore: Int
class var sharedInstance :GameState {
struct Singleton {
static let instance = GameState()
}
return Singleton.instance
}
init() {
// Init
score = 0
highScore = 0
// Load game state
let defaults = NSUserDefaults.standardUserDefaults()
highScore = defaults.integerForKey("highScore")
}
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")
NSUserDefaults.standardUserDefaults().synchronize()
}
func saveHighScore(high:Int64) {
NSUserDefaults.standardUserDefaults()
//check if user is signed in
if GKLocalPlayer.localPlayer().authenticated {
var scoreReporter = GKScore(leaderboardIdentifier: "LeaderboardID")
scoreReporter.value = Int64(highScore) //score variable here (same as above)
var scoreArray: [GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray, withCompletionHandler: {(error : NSError!) -> Void in
if error != nil {
println(error.description)
}
})
}
}
}
EndGameScene
class EndGameScene: SKScene, GKGameCenterControllerDelegate {
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
{
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch = touches as! Set<UITouch>
var location = touch.first!.locationInNode(self)
var node = self.nodeAtPoint(location)
if (node.name == "gameCenter") {
var vc = self.view?.window?.rootViewController
var gc = GKGameCenterViewController()
gc.gameCenterDelegate = self
vc?.presentViewController(gc, animated: true, completion: nil)
}
我该如何解决?谢谢,抱歉我的英文