gpt4 book ai didi

ios - 将变量从 UIViewController 传递到 SK Scene

转载 作者:搜寻专家 更新时间:2023-10-31 23:01:52 24 4
gpt4 key购买 nike

我正在使用 Swift 2 在 XCode 7 中制作游戏。我有一个变量,我想从开始屏幕(它是一个 UIViewController)传递到游戏场景(它是一个 SKScene)。我希望玩家在 UIView 中选择一个角色并在 SKScene 中使用它。我还希望 SKScene 中的分数显示在作为 UIView 的游戏结束屏幕中。我看过在两个 UIViewController 之间和两个 SKScenes 之间传递数据的教程,但它们都不适用于这种情况。

如何将变量从 UIViewController 传递到 SKScene(反之亦然)?

最佳答案

如果您还没有找到答案,我确实找到了一种方法。我在做一些非常相似的事情。

在我的例子中,我有一个 UIView,它从我的场景中作为暂停菜单被调用。我使类名和变量名更加模糊,以便它们也适用于您的情况。

class MyView: UIView {
var scene: MyScene!
var button: UIButton!
init(frame: CGRect, scene: MyScene){
super.init(frame: frame)
self.scene = scene
//initialize the button
button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "buttonTapped"))
}

func buttonTapped(){
self.removeFromSuperview() // this removes my menu from the scene
scene.doTheThing() // this calls the method on my scene
}

这是我场景的相关部分。

class MyScene: SKScene {

func doTheThing(){
//this is a function on the scene. You can pass any variable you want through the function.
}
}

在您的情况下,听起来第一个屏幕是 UIView,第二个屏幕是 SKScene。

您可能想先制作您的 SKScene,暂停它,然后在场景前面添加 UIView。选择角色后,您可以移除 UIView 并将您的角色添加到场景中。

我希望这能回答您的问题。如果它不让我知道。

关于ios - 将变量从 UIViewController 传递到 SK Scene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32672522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com