0

我正在用精灵套件制作游戏,在我使用之前一切都很好GameCenter,这是我的代码

define the Gameviewcontroller:

var GameController:GameViewController = GameViewController()

当您触摸内部的节点时TouchecBegan

    let touchLocation = (touches as NSSet).anyObject()?.locationInNode(self)

    let targetNode = self.nodeAtPoint(touchLocation!)

    if targetNode.name == "leader"{

        print("hhhh")

        GameController.showGamecenetr()

    }

并在GameViewController代码中ShowGameCenter

func showGamecenetr() {

    print("jijijijji")

    GameKitHelper.sharedInstance.showGKGameCenterViewController(self)

}

它应该弹出视图GameCenter,但显示错误:

无法将类型的值UIView转换为SubView

我发现这是一个非常容易的错误,但我以前从未在这里遇到过,我不知道如何处理它,有人可以帮助我吗?

这是里面的代码showGKGameCenterViewController()

func showGKGameCenterViewController(viewController:UIViewController!){

    if !gamecenterEnable {

        print("No user!")
        return 
        }

    let gameCenterViewController = GKGameCenterViewController()

    gameCenterViewController.gameCenterDelegate = self

    gameCenterViewController.viewState = .Leaderboards
    viewController.presentViewController(gameCenterViewController, animated: true, completion: nil)

    }

帮助帮助;(

4

1 回答 1

1

所以我假设你验证了你的本地播放器/用户。要打开GameCenter就在你的场景中这样做

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

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

        if targetNode.name == "leader"{
            let gcViewController: GKGameCenterViewController = GKGameCenterViewController()
            gcViewController.gameCenterDelegate = self

            gcViewController.leaderboardIdentifier = kLeaderboardID

            gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
            let vc: UIViewController = self.view!.window!.rootViewController!
            vc.presentViewController(gcViewController, animated: true, completion: nil)  
        }
    }
}

GKGameCenterControlleDelegate在你的场景中

class GameScene: SKScene, GKGameCenterControllerDelegate {

func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {

    AppDelegate().resumeBackgroundMusic()
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
于 2015-11-26T15:35:51.467 回答