Swift,SpriteKit,GameOver之后的GameViewController和MainViewController之间的争论

时间:2016-05-09 22:33:42

标签: ios swift sprite-kit

我想在游戏结束后关闭一个SKScene并返回主菜单。

从MainViewController到GameViewController,我用prepareForSegue做了一个Storyboard Segue,将一些数据从MainViewController传递给GameViewController

在GameViewController中,我这样做:

    override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene(fileNamed:"GameScene") {
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        skView.ignoresSiblingOrder = true

        scene.scaleMode = .AspectFill

        skView.presentScene(scene)

        scene.viewController = self

    }
}

然后,我在GameScene.swift中声明了View Controller:

weak var viewController:GameViewController? = GameViewController()

我还在GameScene.swift中有以下函数来调用endGameScene.swift:

    func endGame() {
    // 1
    gameOver = true

    // 2
    // Save high score
    GameState.sharedInstance.saveState()

    // 3
    let reveal = SKTransition.fadeWithDuration(0.5)
    let endGameScene = EndGameScene(size: self.size)
    self.view!.presentScene(endGameScene, transition: reveal)

}

最后,endGameScene.swift像这样调用dismissViewControllerAnimated:

weak var viewController:GameViewController? = GameViewController()       

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    // Transition back to the Game
    /*let reveal = SKTransition.fadeWithDuration(0.5)
    let gameScene = GameScene(size: self.size)
    self.view!.presentScene(gameScene, transition: reveal)*/

    print(viewController)
    self.viewController?.dismissViewControllerAnimated(true, completion: nil)

}

不幸的是,print(viewController)返回nil,当然,它不会关闭ViewController。

我在哪里弄错了?

1 个答案:

答案 0 :(得分:0)

创建这样的功能

Doctor    : JOHN A. BROWN

然后只需拨打func goToGameScene(){ let gameScene:GameScene = GameScene(size: self.view!.bounds.size) // create your new scene let transition = SKTransition.crossFadeWithDuration(1.5) // create type of transition (you can check in documentation for more transtions) gameScene.scaleMode = SKSceneScaleMode.Fill self.view!.presentScene(gameScene, transition: transition) } ,即可进行此转换。

您无需关闭任何内容,因为每次转换时都会创建一个新场景。