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 ?
Asked
Active
Viewed 2,248 times
1
-
3Possible duplicate of [SpriteKit: Passing data between scenes](http://stackoverflow.com/questions/18885490/spritekit-passing-data-between-scenes) – Whirlwind Feb 14 '16 at 11:43
-
1Please stop marking and saying that question is dublicate if all answers are in Objective-C. – Tarvo Mäesepp Apr 09 '17 at 17:29
1 Answers
7
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

crashoverride777
- 10,581
- 2
- 32
- 56