使用SpriteKit的游戏中心排行榜完成按钮不起作用

时间:2014-09-22 14:09:19

标签: ios xcode swift game-center

我目前正在使用Swift开发我的第一款iOS游戏。不幸的是,到目前为止,我在实施Gamecenter方面遇到的问题比其他任何问

在弄清楚如何让排行榜弹出并保存高分后,我遇到了下一个问题:

如何使“完成”按钮工作?如果我按它没有任何反应。

首先我的代码:

GameViewController.swift:

import GameKit        

    ....

override func viewWillLayoutSubviews() {

    let skView = self.view as SKView
    skView.ignoresSiblingOrder = true

    let scene = GameScene.sceneWithSize(skView.bounds.size)
    scene.scaleMode = .AspectFill

    skView.presentScene(scene)

    authenticateLocalPlayer()

}

func authenticateLocalPlayer(){
    var localPlayer = GKLocalPlayer()
    println(localPlayer)
    localPlayer.authenticateHandler = {(viewController, error) -> Void in
        if ((viewController) != nil) {
            self.presentViewController(viewController, animated: true, completion: nil)
        }else{

            println((GKLocalPlayer.localPlayer().authenticated))
        }
    }
}

GameScene.swift:

    import GameKit        

    ....

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    var touch:UITouch = touches.anyObject() as UITouch
    var location:CGPoint = touch.locationInNode(self)

    if gameCenterRect.contains(location) {

            var vc = self.view?.window?.rootViewController
            var gc = GKGameCenterViewController()
            vc?.presentViewController(gc, animated: true, completion: nil)

        }

 func saveHighscore(score:Int){

    NSUserDefaults.standardUserDefaults().setObject(score, forKey: "kHighscore")

    if GKLocalPlayer.localPlayer().authenticated {
        var scoreReporter = GKScore(leaderboardIdentifier: "LEADERBOARD_ID")
        scoreReporter.value = Int64(self.highscore)
        var scoreArray: [GKScore] = [scoreReporter]
        //println("report score \(scoreReporter)")
        GKScore.reportScores(scoreArray, {(error : NSError!) -> Void in
            if error != nil {
                println("error")
                //NSLog(error.localizedDescription)
            }
        })

    }
}

我必须在哪里添加一些代码才能返回?

1 个答案:

答案 0 :(得分:4)

你必须将这些行添加到你的游戏场景中:

class GameScene: SKScene, SKPhysicsContactDelegate,UIGestureRecognizerDelegate,     GKGameCenterControllerDelegate{

func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
{
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)

}

并修复此

 if gameCenterRect.contains(location) {

   var vc = self.view?.window?.rootViewController
   var gc = GKGameCenterViewController()
   gc.gameCenterDelegate = self
   vc?.presentViewController(gc, animated: true, completion: nil)

 }