游戏中心"完成"按钮问题?

时间:2016-02-02 22:47:09

标签: ios swift sprite-kit

"完成" Game Center中的按钮不会关闭控制器。解除视图控制器的代码与我看到的完全相同。我在这里看过类似的问题,但没有运气。任何人都可以帮助我吗?

GameScene.Swift(这是我的开始菜单)

class GameScene: SKScene, GKGameCenterControllerDelegate {


var startBtn = SKSpriteNode(imageNamed: "play button")
var title = SKLabelNode(fontNamed:  "AvenirNext-UltraLight")
var leaderboardButtonImage = SKSpriteNode(imageNamed: "leaderboard button")

override func didMoveToView(view: SKView) {

    let bounds = UIScreen.mainScreen().bounds
    self.scene?.size = CGSize(width: bounds.size.width, height: bounds.size.height)

    scene?.backgroundColor = UIColor.whiteColor()

    startBtn.position = CGPoint(x: self.frame.width / 2 - 100, y: self.frame.height / 2)
    startBtn.size = CGSize(width: 140, height: 55)
    self.addChild(startBtn)

    leaderboardButtonImage.position = CGPoint(x: self.frame.width / 2 + 100, y: self.frame.height / 2)
    leaderboardButtonImage.size = CGSize(width: 140, height: 55)
    self.addChild(leaderboardButtonImage)

}





func showLeaderboard() {

    let vc = self.view?.window?.rootViewController
    let gc = GKGameCenterViewController()

    gc.viewState = GKGameCenterViewControllerState.Leaderboards
    gc.leaderboardIdentifier = "I removed this part"
    vc!.presentViewController(gc, animated: true, completion: nil)



}

func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {

    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)


}




override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

    for touch in (touches ) {
        let location = touch.locationInNode(self)

        if (startBtn.containsPoint(location)) {

            self.view?.presentScene(GamePlayScene(), transition: SKTransition.crossFadeWithDuration(1.0))
            GamePlayScene().scaleMode = .ResizeFill
            startBtn.removeFromParent()
            title.removeFromParent()
            leaderboardButtonImage.removeFromParent()

        }

        if (leaderboardButtonImage.containsPoint(location)) {

            showLeaderboard()

        }

    }

}



}

1 个答案:

答案 0 :(得分:1)

在我的func showLeaderboard()中,我不得不添加 gc.gameCenterDelegate = self 这一行,使“完成”按钮有效。

我希望有同样问题的其他人看到这种潜在的解决方法。

func showLeaderboard() {

let vc = self.view?.window?.rootViewController
let gc = GKGameCenterViewController()

gc.viewState = GKGameCenterViewControllerState.Leaderboards
gc.gameCenterDelegate = self
gc.leaderboardIdentifier = "I removed this part"
vc!.presentViewController(gc, animated: true, completion: nil)

}