0

I'm making some great progress with my first iOS game app, currently I'm working on the high score element and if anyone would be so kind as to help guide me how to use the correct code.

I've followed a few tutorials with mixed results, so far I'm increasing the score count by 1 for each screen press (will be changed but using for the purpose for this post), this works fine however I need some help saving both the high score using NSUserDefaults and also displaying the high score.

Below is the extract of my code, currently the score works fine however the high score remains at 0 and doesn't increase. Any help would be appreciated!

Here is my code.....

Thank you.

EDITED WITH REWORKED CODE

// in gameScene:

var scoreLabelNode = SKLabelNode()
var score = NSInteger()
var highScoreLabelNode = SKLabelNode()
var highScore = NSInteger()

// in did move to view

    score = 0
    scoreLabelNode.fontName = "Helvetica-Bold"
    scoreLabelNode.position = CGPoint(x: self.frame.size.width / 2.8, y: self.frame.size.height / 1.2 )
    scoreLabelNode.fontSize = 20
    scoreLabelNode.alpha = 0.2
    scoreLabelNode.zPosition = -30
    scoreLabelNode.text = "Score \(score)"
    self.addChild(scoreLabelNode)

// start of high score

    var highScoreDefault = NSUserDefaults.standardUserDefaults()

    if (highScoreDefault.valueForKey("highScore") != nil){
        highScore = highScoreDefault.valueForKey("highscore") as! NSInteger!
        highScoreLabelNode.text = NSString(format: "highscore : %i", highScore) as String
    }

//        highScore = 0
//        if (score > highScore) {
//            highScore = score
//            highScoreLabelNode.text = NSString(format: "highscore : %i", highScore) as String
//        }


    highScoreLabelNode.fontName = "Helvetica-Bold"
    highScoreLabelNode.position = CGPoint(x: self.frame.size.width / 2.6, y: self.frame.size.height / 1.3 )

    highScoreLabelNode.fontSize = 20
    highScoreLabelNode.alpha = 0.2
    highScoreLabelNode.zPosition = -30
    highScoreLabelNode.text = "High Score \(score)"
    self.addChild(highScoreLabelNode)

// in touches began 

            let incrementScore = SKAction.runBlock ({
                ++self.score
                self.scoreLabelNode.text = "Score \(self.score)"
            })

            self.runAction(incrementScore)

// in update before each frame

//    highScore = 0
if (score > highScore) {
    highScore = score
    highScoreLabelNode.text = NSString(format: "highscore : %i", highScore) as String
}
Rich Townsend
  • 571
  • 1
  • 5
  • 21

2 Answers2

1

To me it seems you dont ever call

 if (score > highScore) {
    highScore = score
    highScoreLabelNode.text = NSString(format: "highscore : %i", highScore) as String
}

again anywhere, which is where your highscore is set? Is it called somewhere that is not indicated in the code you have provided?

dorian
  • 847
  • 5
  • 11
  • thanks for the reply, there isn't any more code and your right I don't call the code at all, how and where would you suggest I could do this? – Rich Townsend Aug 05 '15 at 21:21
  • 1
    you preferably should put that part into a method. call it func updateHighScore for example. And call that method whenever you are ready to check if your current score is higher than the highscore. For example: when the game ends (when player dies or whatever) – dorian Aug 05 '15 at 21:24
  • please mark my reply as the answer if that answered your question. If you have any further questions, i can try to help. – dorian Aug 05 '15 at 21:57
  • That worked perfectly, thank you so much. I have the slight issue of the highscore not saving, would you know what i need to change in my code to fix? – Rich Townsend Aug 06 '15 at 06:04
  • you should look into saving into NSUserDefaults. Look into this answer http://stackoverflow.com/questions/3074483/save-string-to-the-nsuserdefaults – dorian Aug 06 '15 at 08:36
  • mark my reply as answer please. Thants how stackoverflow works ;) – dorian Aug 06 '15 at 08:38
0

From what I can see you are not saving the highscore to User Defaults with NSUserDefaults.standardUserDefaults.setObject(), also, I'm not sure why you are setting highScore to 0

Josh
  • 197
  • 1
  • 4
  • 15