Swift:使用StoryBoards和UIViewControllers在GameScene中满足条件后返回主菜单?

时间:2017-01-22 11:04:12

标签: swift uiviewcontroller sprite-kit uistoryboard skspritenode

上下文

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

看一下Main.storyboard,我们看到以下内容: enter image description here

虽然这个故事板允许一个人通过点击从左到右进展:

play - > (简单/困难) - > GameScene

UIViewController - > MyUIViewController - > GameViewController

并点击上一个UIButton上的UIViewController并使用UILabel说明"得分"从右到左,没有办法从{到使用"分数" {1}}到最后GameViewController UIViewController

问题

鉴于在UILabel期间发生某个事件,我如何从GameScene传递一些信息(例如分数)并转换到最后一个GameScene,以便用户可以再次开始游戏。除了删除UIViewController中的所有内容之外,还要删除GameViewController的无法访问的旧实例。

对于某些条件,让它在M.W.E中点击紫色精灵。对于这些信息,让它变得微不足道,比如直到精灵被按下的时间,甚至只是一个随机数。

最低工作范例

Stack Overflow SKSpriteKit

注意

我的回答here - 并在此处实施StackOverflow SKSpriteKit with Menu - 表明这显然是可行的,但将所有内容降级为GameScene而不是使用SKView。如果可能的话,请保留与使用此混合storyboard - storyboard方法相关的答案以及如何让SKView调用"得分" - {{1} }。

1 个答案:

答案 0 :(得分:1)

您可以使用下一个场景的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)")
}