SpriteKit 从 SKScene 回到菜单

SpriteKit back to menu from SKScene

我正在学习 Swift 和 SpriteKit,目前面临一个问题。基本上我正在尝试将可点击的 SKSpriteNode 实现为 "back to menu" 并重置游戏。 到目前为止我的代码: 在 GameScene.swift

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        //let node = self.nodes(at: location)
        let node : SKNode = self.atPoint(location)

        if node.name == "back_button" {

            let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = mainStoryboard.instantiateViewController(withIdentifier: "MenuVC_ID")
            gameVC?.dismiss(animated: true, completion: nil)
            self.view?.window?.rootViewController?.present(vc, animated:true, completion: nil)
            self.view?.presentScene(nil)
        }
    }
}

MenuVC.swift

class MenuVC : UIViewController {
    @IBAction func Player2(_ sender: Any) {
        moveToGame(game: gameType.player2)
    }
    @IBAction func easy(_ sender: Any) {
        moveToGame(game: gameType.easy)
    }
    @IBAction func medium(_ sender: Any) {
        moveToGame(game: gameType.medium)

    }
    @IBAction func hard(_ sender: Any) {
        moveToGame(game: gameType.hard)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print ("somtu")
    }
    func moveToGame(game: gameType) {
        print("movetogame")
        let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gameVC") as! GameViewController
        currentGameType = game
        self.navigationController?.pushViewController(gameVC, animated: true)
    }

每当我在游戏过程中单击 back_button 时,都会显示 MenuVC,但随后单击按钮时没有任何反应。我尝试了其他帖子的一些建议,但没有任何效果。 谢谢你的回答。

你的代码很混乱。如果您的项目使用通用结构:

rootViewController-->rootView(SKView)-->scene(SKScene/MenuScene,GameScene,etc.)

您可以使用 SKView.presentScene(SKScene, transition: SKTransition) 方法切换场景并通过将父场景变量传递给 sub-scene 或使用 Notification 触发 rootViewController 呈现相应场景来启用 return 功能。

如果您的项目用视图控制器包裹了每个场景:

MenuVC(MenuScene)-->GameVC(GameScene)

您可以像普通应用一样控制应用流程-

  1. navigationController.push/navigationController.pop
  2. viewController.present/viewController.presentingVC.dismiss
  3. self.view?.window?.rootViewController=对应视图控制器