Swift:在 GameScene 中使用故事板和 UIViewController 满足条件后返回主菜单?

Swift: returning to Main Menu after condition is met in GameScene using StoryBoards and UIViewControllers?

上下文

此题与Swift: SKSpriteKit, using Storyboards, UIViewController and UIButton to set in game parameters?相关,使用相同的M.W.E。 (下面link)。

看看 Main.storyboard 我们看到以下内容:

虽然此故事板允许通过单击从左到右前进:

播放 -> (easy/hard) -> GameScene

UIViewController -> MyUIViewController -> GameViewController

然后从右到左单击最后一个 UIViewController 上的 UIButtonUILabel 表示“分数”,无法从 GameViewController 到最后 UIViewController 与“得分” UILabel.

问题

鉴于 GameScene 期间发生的某个事件,我如何从 GameScene 传递一些信息(例如分数)并过渡到最后一个 UIViewController 以便用户可以重新开始游戏。除了删除 GameViewController 中的所有内容之外,不只是以 GameScene.

的无法访问的旧实例结束

对于特定条件,假设是点击 M.W.E 中的紫色精灵。对于信息,让它成为一些微不足道的东西,比如按下精灵之前的时间,或者甚至只是一个随机数。

最小工作示例

Stack Overflow SKSpriteKit

备注

我的回答 - and implemented hereWhosebug SKSpriteKit with Menu - 表明这显然是可能的,但是通过将所有内容归入 SKView 而不是使用 storyboard。因此,如果可能,请保留与使用这种混合 storyboard-SKView 方法以及如何让 GameScene 调用“分数”-UIViewController.[=37 相关的答案=]

您可以使用下一个场景的 userData 实例 属性 来存储当前场景,例如:

nextScene.userData = NSMutableDictionary()
nextScene.userData?.setObject(actualScore, forKey: "actualScore" as NSCopying)

当你在下一个场景时,你可以向这个 NSMutableDictionary 询问前一个场景是谁:

if let actualScore = self.userData?.value(forKey: "actualScore") {
    print("well done!, the score was: \(actualScore)")
}