如何用Swift从另一个类调用字符串?

时间:2023-01-23 00:26:17

Specifically, SpriteKit.

具体来说,SpriteKit。

In GameScene I set the variable:

在GameScene中,我设置了变量:

var score = Int()

which is constantly being changed. In EndScene.swift, I want to print that score in a label.

这是不断变化的。在EndScene。斯威夫特,我想把分数印在标签上。

What I have for the label usually is:

我的标签通常是:

scoreLabel.text = "SCORE:" + String(score)

But EndScene doesn't recognize score as a value from GameScene. How can I call 'score' in that string?

但是EndScene并不承认score是GameScene的一个值。如何在字符串中调用'score' ?

In GameScene I have a func called

在GameScene,我有一个func。

func updateScore(_ scoreToAdd:Int) {

func updateScore(_ scoreToAdd:Int){

    score = score + scoreToAdd

    scoreLabel.text = "Score: " + String(score)

}

That's what score in EndScore should equal. How can I do that?

这是EndScore应该等于的。我怎么做呢?

3 个解决方案

#1


1  

You need to make sure that score is defined as public in GameScene.swift so you can access it outside the GameScene class -

您需要确保在GameScene中将分数定义为public。你可以在GameScene课堂之外访问它。

public var score = Int()

Then make sure that you access it with the GameScene object -

然后确保您使用GameScene对象-访问它

let game = GameScene()
scoreLabel.text = "SCORE:" + String(game.score)

#2


0  

You should use NSArchiver to save to a file you value and retrieve it where do you want but it's a long method just to save a single parameter as you wish, so you could save your score value through UserDefaults (knowed also as Next Step User Defaults or NSDefaults). It's fast and easy to use.

您应该使用NSArchiver将值保存到您想要的文件中并在需要的地方检索它,但是这是一个很长的方法,只需要按照您的意愿保存一个参数,这样您就可以通过UserDefaults(也知道作为下一步的UserDefaults或NSDefaults)保存您的score值。它又快又好用。

// Save score value
UserDefaults.standard.set(score, forKey:"score")
UserDefaults.standard.synchronize()

// Retrieve score value
if let savedScore = UserDefaults.standard.object(forKey: "score") {
    print(savedScore)
}

#3


0  

When you are moving to your EndScene, use the userData to transfer data.

当您移动到您的EndScene时,使用userData来传输数据。

func movingToEndScene()
{
    let endScene = EndScene(...)
    let userData = ["score":score]
    endScene.userData = userData
    self.view.presentScene(endScene)
}

Then, inside your end scene, you can do whatever you want with it:

然后,在你的最终场景中,你可以用它做任何你想做的事情:

scoreLabel.text = "\(self.userData["score"])"

#1


1  

You need to make sure that score is defined as public in GameScene.swift so you can access it outside the GameScene class -

您需要确保在GameScene中将分数定义为public。你可以在GameScene课堂之外访问它。

public var score = Int()

Then make sure that you access it with the GameScene object -

然后确保您使用GameScene对象-访问它

let game = GameScene()
scoreLabel.text = "SCORE:" + String(game.score)

#2


0  

You should use NSArchiver to save to a file you value and retrieve it where do you want but it's a long method just to save a single parameter as you wish, so you could save your score value through UserDefaults (knowed also as Next Step User Defaults or NSDefaults). It's fast and easy to use.

您应该使用NSArchiver将值保存到您想要的文件中并在需要的地方检索它,但是这是一个很长的方法,只需要按照您的意愿保存一个参数,这样您就可以通过UserDefaults(也知道作为下一步的UserDefaults或NSDefaults)保存您的score值。它又快又好用。

// Save score value
UserDefaults.standard.set(score, forKey:"score")
UserDefaults.standard.synchronize()

// Retrieve score value
if let savedScore = UserDefaults.standard.object(forKey: "score") {
    print(savedScore)
}

#3


0  

When you are moving to your EndScene, use the userData to transfer data.

当您移动到您的EndScene时,使用userData来传输数据。

func movingToEndScene()
{
    let endScene = EndScene(...)
    let userData = ["score":score]
    endScene.userData = userData
    self.view.presentScene(endScene)
}

Then, inside your end scene, you can do whatever you want with it:

然后,在你的最终场景中,你可以用它做任何你想做的事情:

scoreLabel.text = "\(self.userData["score"])"