【问题标题】:Swift: returning to Main Menu after condition is met in GameScene using StoryBoards and UIViewControllers?Swift:使用 StoryBoards 和 UIViewControllers 在 GameScene 中满足条件后返回主菜单?
【发布时间】:2017-01-22 11:04:11
【问题描述】:

上下文

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

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

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

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

UIViewController -> MyUIViewController -> GameViewController

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

问题

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

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

最小工作示例

Stack Overflow SKSpriteKit

注意

我的回答 here - 并在此处实现StackOverflow SKSpriteKit with Menu - 表明这显然是可能的,但是通过将所有内容归入SKViews 而不是使用storyboard。因此,如果可能,请保留与使用这种混合 storyboard-SKView 方法以及如何让 GameScene 调用“分数”-UIViewController 相关的答案。

【问题讨论】:

  • 您熟悉委托模式吗?这是子视图与父视图通信的常用方式。这个SO Post 展示了基本思想并执行了展开序列。委托协议可以扩展为包含一个 score 属性,因此当值发生变化时,场景可以通知​​它的 ViewController。
  • @MarkBrownsword 不。我不是,你能用这个 M.W.E. 解释一下吗:github.com/SumNeuron/StackOverflow-SKSpriteKit-with-Menu

标签: swift uiviewcontroller sprite-kit uistoryboard skspritenode


【解决方案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)")
}

【讨论】:

    猜你喜欢
    • 2022-10-06
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2023-02-09
    相关资源
    最近更新 更多