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.
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.