游戏结束后从GameScene更改为UIViewController

时间:2018-07-14 11:17:35

标签: sprite-kit swift4

GameScene中的游戏结束后,视图应返回到我的HomeViewController,但它所做的只是一个SIGBRT-错误

在GameScene中,我有以下代码:

var gameEnded: (() -> Void)?

游戏结束后:

self.gameEnded?()

并在GameViewController中:

scene.gameEnded = {
    let homeViewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
    self.present(homeViewController, animated: false, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

可能您应该尝试委托:

//: Playground - noun: a place where people can play

import UIKit
import SpriteKit

protocol GameSceneDelegate: class {

    func gameSceneDidFinished(scene: GameScene)
}

class GameScene: SKScene {

    weak var controllerDelegate: GameSceneDelegate?
}

class GameViewController: UIViewController {


    override func viewDidLoad() {

        guard let skView = self.view as? SKView else {
            return
        }

        let gameScene = GameScene(size: skView.bounds.size)
        gameScene.controllerDelegate = self
        skView.presentScene(gameScene)
    }
}

extension GameViewController: GameSceneDelegate {

    func gameSceneDidFinished(scene: GameScene) {

        let homeViewController = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
        self.present(homeViewController, animated: false, completion: nil)
    }
}

class HomeViewController: UIViewController {

}