【问题标题】:SpriteKit back to menu from SKSceneSpriteKit 从 SKScene 返回菜单
【发布时间】:2017-02-06 23:26:35
【问题描述】:

我正在学习 Swift 和 SpriteKit,目前面临一个问题。基本上我正在尝试将可点击的 SKSpriteNode 实现为“返回菜单”并重置游戏。 到目前为止我的代码: 在 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 就会出现,但在按钮点击时什么也没有发生。我尝试了其他帖子中的几个建议,但没有一个能正常工作。 谢谢你的回答。

【问题讨论】:

    标签: swift uiviewcontroller sprite-kit skspritenode


    【解决方案1】:

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

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

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

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

    MenuVC(MenuScene)-->GameVC(GameScene)

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

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

    【讨论】:

    • 我将 self.navigationController?.pushViewController(gameVC, animated: true) 更改为 self.view.window?.rootViewController = gameVC ,它的工作原理就像魅力一样。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多