Search code examples
swiftsprite-kitnsuserdefaultssklabelnode

Connecting an SKLabelNode to a game over SKScene label text


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.


Solution

  • 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")