3
votes

I am building a game in spritekit that has a three scenes: main menu, play scene, and game over scene.

I need the main menu to display the high score, the play scene to display a current score, and the game over scene to display the high score and the player's score from their last play through.

How would I transfer this score data between scenes and how would I keep track of a player's high score?

Looking around I've found stuff about NSUserDefaults but I don't know how to implement it properly.

Admittedly I am a novice at SpriteKit and coding in general so I need very specific instructions.

Sorry if this question is rather broad. Thanks.

1

1 Answers

3
votes

NSUserDefaults is a great way to keep track of scores.

To save the high score:

let x : Int = 45 // This int is your high score
var myString = String(x) // This String is you high score as a String

var defaults = NSUserDefaults.standardUserDefaults()

defaults.setObject(myString, forkey : "High_Score") // Saving the String to NSUserDefaults

To access the high score:

var defaults = NSUserDefaults.standardUserDefaults()

var HighScore = defaults.objectForKey("High_Score") // Retrieving your high score as a String