无法将变量传递给swift中的spritekit中的场景

时间:2023-01-22 21:20:56

I'm currently having trouble with passing variables to a scene in spritekit (swift). In android when moving to a different activity, I just set the parameter to the Intent and simply get the variable. But since I'm new to spritekit and swift, i find it difficult. Is there a clear way to pass a variable to the scene ? I have a sample code below that I tested but did not work.

我目前在spritekit(swift)中将变量传递给场景时遇到了麻烦。在android中移动到不同的活动时,我只是将参数设置为Intent并简单地获取变量。但由于我是spritekit和swift的新手,我觉得很难。有没有明确的方法将变量传递给场景?我有一个示例代码,我测试但没有工作。

class GameSKView: SKView {

    var mylevel = 0

}

//Inside my main GameScen 
let gameView = self.view as GameSKView
sample.text = "my level is :\(gameView.mylevel)"

2 个解决方案

#1


0  

Try this:

尝试这个:

class GameSKView: SKView {

    internal var mylevel = 0

}

And inside you main game scene

在你的主要游戏场景里面

if let gameView = self.view as GameSKView {
    sample.text = "my level is :\(gameView!.mylevel)"
}

#2


0  

Finally found the solution.

终于找到了解决方案。

In order to use the custom SKView, I have done the following procedures.

为了使用自定义SKView,我已经完成了以下过程。

  1. Open the Idenity inspector in the Main.storyboard

    在Main.storyboard中打开Idenity检查器

  2. Replace the [SKView] inside the Custom Class with your [customSKView] , In my case GameSKView

    用[customSKView]替换自定义类中的[SKView],在我的例子中是GameSKView

  3. Save. That was all I needed to do.

    保存。这就是我需要做的一切。

#1


0  

Try this:

尝试这个:

class GameSKView: SKView {

    internal var mylevel = 0

}

And inside you main game scene

在你的主要游戏场景里面

if let gameView = self.view as GameSKView {
    sample.text = "my level is :\(gameView!.mylevel)"
}

#2


0  

Finally found the solution.

终于找到了解决方案。

In order to use the custom SKView, I have done the following procedures.

为了使用自定义SKView,我已经完成了以下过程。

  1. Open the Idenity inspector in the Main.storyboard

    在Main.storyboard中打开Idenity检查器

  2. Replace the [SKView] inside the Custom Class with your [customSKView] , In my case GameSKView

    用[customSKView]替换自定义类中的[SKView],在我的例子中是GameSKView

  3. Save. That was all I needed to do.

    保存。这就是我需要做的一切。