Swift: How to Track and Report Grades Between Scenes in SpriteKit

I am creating a game in spritekit that has three scenes: the main menu, the game scene, and the game on top of the scene.

I need the main menu to display the high score, the game scene to display the current score, and the game above the scene to display the high score and player score from their last game.

How do I pass rating data between scenes and how will I track a player's high score?

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

I am admittedly new to SpriteKit and coding in general, so I need very specific instructions.

Sorry if this question is rather broad. Thank.

+3


source to share


1 answer


NSUserDefaults is a great way to track results.

To keep your score high:

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 get a high score:

var defaults = NSUserDefaults.standardUserDefaults()

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

      

+3


source







All Articles