Reputation: 11
I am using NSUserDefaults to display the score from my GameScene
on my resetViewController
. The problem is that if the user doesn't score it will display the players last score.
In GameScene
var score: Int = 0
var scoreLabel = SKLabelNode()
let defaults = NSUserDefaults.standardUserDefaults()
score++
scoreLabel.text = "\(score)"
defaults.setObject(scoreLabel.text, forKey: "scoring")
In resetViewController
var score = defaults().stringForKey("scoring")
YourScore.text = score
I'm sure it's something simple but I can't seem to figure it out
Upvotes: 0
Views: 55
Reputation: 49
I suggest saving the integer, not the string. That way the default will be zero, and you can easily reset the score by setting the key to zero.
NSUserdefaults.standardUserDefaults().IntegerForKey("score")
NSUserdefaults.standardUserDefaults().setInteger(0, forKey: "score")
Upvotes: 0
Reputation: 52565
removeObjectForKey:
removes the value from NSUserDefaults. You could also set it to zero. Or just not use NSUserDefaults -- there's not much point in using it unless you need to persist the value between restarts of your app.
Upvotes: 0
Reputation: 22641
When you need to score to reset, just call
NSUserDefaults.standardUserDefaults().setObject(nil, forKey: "scoring")
Upvotes: 1