0

I have an SKLabelNode that increments by one point every time my player touches an object:

let scoreLabel = childNodeWithName("points") as! Points
scoreLabel.increment()

However, I have an SKScene that pops up when my player hits an enemy. In the SKScene, I have labels set up for "High score", regular scoring and a tap to play again:

score.text = String(format: "%d", pointsLabel)
highScore.text = String(format: "%d", pointsLabel) //more of this code is in my GameScene

How do I connect the points label and highscore label with the pointsLabel() SKLabelNode to make the score and high score show up in my SKScene? Will post more code if necessary.

Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
Rae Tucker
  • 523
  • 1
  • 6
  • 16

1 Answers1

1

If you want to share the score among several scenes you can save the value with NSUserDefaults.

Saving

let score = 123
NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "score")

Reading

let score = NSUserDefaults.standardUserDefaults().integerForKey("score")
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • Okay, thank you(: where would I put this in my code? – Rae Tucker Dec 08 '15 at 18:05
  • Each time you update the `score`, save a copy of it on `NSUserDefaults`. This way when you read that value from another scene you get the updated score. Please consider that value stored in `NSUserDefaults` will be available also after an app restart. – Luca Angeletti Dec 08 '15 at 18:06