ios - HighScore Not Displaying In Menu Scene -


i have app score , highscore system. can display highscore within gameplay scene, shows 0 in menu scene. in gameplay scene create variable

var highscore = 0 

then say

if (score > highscore) { score = highscore 

i display inside scene, go menu scene , accesses saying

highscorelabel = actionscene.highscore 

(actionscene being name of file game play scene.) since original value of highscore 0, displays 0. how can fix or program in different way?

you can use nsuserdefaults easiest solution. in gamescene:

let defaults = nsuserdefaults.standarduserdefaults()  defaults.setinteger(10, forkey: "highscore")  defaults.synchronize() 

in scene (menuscene) :

 let defaults = nsuserdefaults.standarduserdefaults()  let highscore = defaults.integerforkey("highscore") 

also note synchronize() method, automatically invoked @ periodic intervals, , keeps in-memory cache in sync user’s defaults database.

ideally, let system worry when persistent storage updated, there cases when want , call synchronize() manually:

because method automatically invoked @ periodic intervals, use method if cannot wait automatic synchronization (for example, if application exit) or if want update user defaults on disk though have not made changes.


Comments