下面我有两种半不同的方法来使用 View Controller 和 SKScene 设置我的游戏。我还没有投入太多时间,因为我希望其他解决方案之一能够发挥作用。 下面更详细地描述的是:
- MainViewController -> GameViewController(保存 SKScene)
- GameViewController(保存 SKScene)- 使用协议(protocol)
0.5 两个 SKScene/两个 swift 自定义类。
第一
MainViewController -> GameViewController(保存 SKScene)
当 SKScene 中的游戏结束时 - 我想关闭 GameViewController 并返回到 MainViewController。
我没有运气让它发挥作用。我尝试过关闭 View Controller ,最接近的是使用类似的东西来关闭 SKScene(在 SKScene 中:
self.view?presentScene(nil)
或
self.dismiss(animated: false, completion: nil)
self.presentingViewController?.dismiss(animated: false, completion: nil)
但我无法返回到 MainViewController。我尝试创建对 GameViewController 的引用来弹出它,如下所示:
weak var gameViewController:GameViewController? = GameViewController()
不幸的是,我无法找到正确的函数来正确地关闭它。
经过大量搜索但没有找到答案后,我想我应该尝试不同的方法。
数字2
GameViewController(持有 SKView)然后让 GVC 持有我想要的按钮和标签并在顶部显示场景。这是一个失败,因为按钮和标签没有 zPositions 并且出现在场景的顶部。
在调用场景之前,我尝试对它们执行 .isHidden = true
,这似乎有效,但是当我关闭场景并尝试将它们更改回 .isHidden = false
code> (尝试使用协议(protocol))我无法让它工作。 (偏离了 stvn 描述的 How to reference the current view controller from a sprite kit scene )
我怀疑第二种方法可能是一种好方法,但也许我只是没有正确地处理它。
数字0.5
最后我现在想到的就是让第二个场景来回“翻转”。我相信这是推荐的方法,但我不太确定如何处理它。如果我做这个版本的话。我希望每个 View 都有一个 gameScene.swift/gameScene.sks (也许使用类似 presenting a uiviewcontroller for skscene 的东西。
我更喜欢第一种方法,即使用两个 View Controller ,因为我认为使用几个 View Controller 可以更轻松地完成游戏的布局。
我最不喜欢的方法是多个场景,因为我不太擅长 Sprite 套件和数学来布置所有内容。
第一种方法可行吗?如果是这样,任何建议都会很棒。如果不是,推荐的方法是什么?
最佳答案
在方法 1 中,如果您的 MainViewController 是 UINavigationController,则推送和弹出您需要的任何其他 ViewController 应该没有问题。
例如在 GameViewController 中:
self.navigationController!.popViewController(animated: true)
关于ios - 在 Swift 中使用多个 ViewController 和 SKScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46702006/