Swift:在使用 StoryBoards 和 UIViewControllers 在 GameScene 中满足条件后返回主菜单? [英] Swift: returning to Main Menu after condition is met in GameScene using StoryBoards and UIViewControllers?

查看:19
本文介绍了Swift:在使用 StoryBoards 和 UIViewControllers 在 GameScene 中满足条件后返回主菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

这个问题与

虽然这个故事板允许用户通过点击从左到右进行:

播放 ->(简单/困难)->GameScene

UIViewController ->MyUIViewController ->GameViewController

然后从右到左单击最后一个 UIViewController 上的 UIButtonUILabel 表示Score",没有使用Score"从 GameViewController 到最后一个 UIViewController 的方法UILabel.

问题

给定在 GameScene 期间发生的某个事件,我如何从 GameScene 传递一些信息(例如分数)并过渡到最后一个 UIViewController 以便用户可以重新开始游戏.除了删除 GameViewController 中的所有内容,以免导致 GameScene 的旧实例无法访问.

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

最小工作示例

堆栈溢出 SKSpriteKit

注意

我的回答此处 - 并在此处实施StackOverflow SKSpriteKit with Menu - 表明这显然是可能的,但是通过将所有内容降级到 SKViews 而不是使用 故事板.因此,如果可能,请保留与使用这种混合 storyboard-SKView 方法以及如何让 GameScene 调用Score"相关的答案.-UIViewController.

解决方案

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

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

当你进入下一个场景时,你可以向这个 NSMutableDictionary 询问谁是上一个场景:

if let actualScore = self.userData?.value(forKey: "actualScore") {打印(干得好!,得分为:\(actualScore)")}

Context

This question is related to Swift: SKSpriteKit, using Storyboards, UIViewController and UIButton to set in game parameters? and uses the same M.W.E. (link below).

Taking a look at the Main.storyboard we see the following:

While this storyboard would allow one to progress from left to right by clicking:

play -> (easy/hard) -> GameScene

UIViewController -> MyUIViewController -> GameViewController

and from right to left by clicking the UIButton on the last UIViewController with the UILabel stating "Score", there is not a way to get from GameViewController to the last UIViewController with the "Score" UILabel.

Question

Given a certain event occurring during the GameScene, how can I pass some information (e.g. a score) from the GameScene and transition to the last last UIViewController so that the user can start the game over again. In addition to deleting everything in the GameViewController as to not just end up with unreachable old instances of the GameScene.

For the certain condition, let that be tapping the purple sprite in the M.W.E. For the information, let that be something trivial like the time until the sprite was pressed, or even just a random number.

Minimum Working Example

Stack Overflow SKSpriteKit

Note

My answer here - and implemented hereStackOverflow SKSpriteKit with Menu - shows that this is clearly possible, but by relegating everything to SKViews rather than using a storyboard. So if possible, please keep answers related to using this hybrid storyboard-SKView approach and how to have the GameScene call up the "Score"-UIViewController.

解决方案

You could use the userData instance property of your next scene to store the current scene, something like:

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

and when you are in the next scene you can ask to this NSMutableDictionary who is the previous scene as:

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

这篇关于Swift:在使用 StoryBoards 和 UIViewControllers 在 GameScene 中满足条件后返回主菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆