0

Apologies if Ive already asked this, but Im having a very hard time.

I have an SKLabelNode set up like this:

    var label = SKLabelNode(fontNamed: "Baveuse")
 class GameScene: SKScene {

  func pointsLabel() -> SKLabelNode {


    label.position = CGPointMake(-80, 500)
    label.fontSize = 50.0
    label.name = "points"
    label.fontColor = UIColor.blackColor()

return label
  }

And incremented like this:

  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        numPoints++
        label.text =  "\(numPoints)"


}

In a separate SKScene, I have it set up like so:

    let score = SKLabelNode(fontNamed: "Baveuse")

    score.fontSize = 70.0
    score.fontColor = SKColor.blackColor()
    score.position = CGPointMake(size.width / 2, 500)
    score.text = "\(numPoints)"
    addChild(score)




  let highScore = SKLabelNode(fontNamed: "Baveuse")
    highScore.fontSize = 40.0
    highScore.fontColor = UIColor(red: 88.0/255.0, green: 148.0/255.0, blue:87.0/255.0, alpha: 1.0)

        highScore.text = String(format: "Best: %d", numPoints)

   }

How would I make it so that it would take the highest score from numPoints and keep it stored until another high score was made? Will post mode code if necessary.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Rae Tucker
  • 523
  • 1
  • 6
  • 16
  • is this a serious question? If you know how to make a variable to score, you should know how to make a variable for high score, and be able to set the high score when score is higher than it – Knight0fDragon Dec 13 '15 at 08:40

1 Answers1

1

Use NSUserDefaults for this (as you´ve added a tag for). When a game ends add this if-statement.

if (NSUserDefaults.standardUserDefaults().doubleForKey("highScore") >= numPoints){
   // New highscore
   NSUserDefaults.standardUserDefaults().setValue(numPoints, forKey: "highScore")
}

Here you check if the numPoints value is greater or equal (you have to check if you want to use equal) than the value in your NSUserDefaults highscore then you update the highscore.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107