Reputation: 33
I created a score variable in my GameScene. Now I want to see the result in another scene, for example GameOverScene. How I can do it ?
Upvotes: 1
Views: 2349
Reputation: 10674
You need always try to post some code on stack overflow.
There is loads of way to do what you want.
1) You could use NSUserDefaults, to save the score and access the saved property in another scene and than assigns it to a new score variable.
2) You could make the score property static, so in gameScene you would say
static var score = 0
and than anywhere in your project you can say
GameScene.score = 5
Remember to reset your score to 0 after every game because static properties are having 1 instance only, i.e. they exist for the lifetime of the app.
3) Yet another way is to do a singleton class
class GameData {
static let shared = GameData()
var score = 0
private init() { }
}
Than in your SKscenes you either say
let gameData = GameData.shared
gameData.score = 5
or
GameData.shared.score = 5
Upvotes: 8