Display high score in a new scene with NSUserDefault in swift spriteKit

332 views Asked by At

I'm making a game with SpriteKit and it saves the player's high score. When the game ends it transitions into a different scene (endScene). I can't figure out how to display the high score in the endScene. Code I have for my high score:

func updateHighScore(){
    //save current points label value
    let pointsLabel = childNodeWithName("pointsLabel") as! DDPointsLabel
    let highScoreLabel = childNodeWithName("highScoreLabel") as! DDPointsLabel

    if highScoreLabel.number < pointsLabel.number {
        highScoreLabel.setTo(pointsLabel.number)

        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setInteger(highScoreLabel.number, forKey: "highscore")
    }
}

func loadHighScore(){
    let defaults = NSUserDefaults.standardUserDefaults()
    let highScoreLabel = childNodeWithName("highScoreLabel") as! DDPointsLabel
    highScoreLabel.setTo(defaults.integerForKey("highscore"))

}
2

There are 2 answers

0
Seth Rodgers On

To load the highscore, you are going to need a int to hold the highscore, I'm not sure what you are trying to do with the highScoreLabel, but to load it you would do var highScore = defaults.integerForKey("highScore") and for your label you can do highScoreLabel.text = "Score \(highScore)"

0
AudioBubble On

This answer assumes that you are properly displaying the score in the GameScene. If so, then in addition to the GameScene file, create two other Swift files: 1) GameState.swift and 2) EndGameScene.swift.

Inside GameState.swift, create a shared instance like this ...

class GameState {
  var score: Int
  var highScore: Int


  class var sharedInstance: GameState {
    struct Singleton {
      static let instance = GameState()
    }

    return Singleton.instance
  }


    init() {
      score = 0
      highScore = 0

      // Load game state
      let defaults = NSUserDefaults.standardUserDefaults()

      highScore = defaults.integerForKey("highScore")
      stars = defaults.integerForKey("stars")
    }

    func saveState() {

      // Update highScore if the current score is greater
      highScore = max(score, highScore)

      // Store in user defaults
      let defaults = NSUserDefaults.standardUserDefaults()
      defaults.setInteger(highScore, forKey: "highScore")
      defaults.setInteger(stars, forKey: "stars")
      NSUserDefaults.standardUserDefaults().synchronize()
    }
}

Inside of the GameScene file, make sure you properly created the labels. If so, then find your code that creates points for your player. Perhaps where there is a collision. Then add the GameState shared instance. It will look something like this ...

lblStars.text = String(format: "X %d", GameState.sharedInstance.stars)
lblScore.text = String(format: "%d", GameState.sharedInstance.score)

In your EndGameScene, make sure you call GameState.sharedInstance with your labels. The code will look something like this ...

    // Score
    let lblScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
    lblScore.fontSize = 60
    lblScore.fontColor = SKColor.whiteColor()
    lblScore.position = CGPoint(x: self.size.width / 2, y: 300)
    lblScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center
    lblScore.text = String(format: "%d", GameState.sharedInstance.score)
    addChild(lblScore)

    // High Score
    let lblHighScore = SKLabelNode(fontNamed: "ChalkboardSE-Bold")
    lblHighScore.fontSize = 30
    lblHighScore.fontColor = SKColor.cyanColor()
    lblHighScore.position = CGPoint(x: self.size.width / 2, y: 150)
    lblHighScore.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center
    lblHighScore.text = String(format: "High Score: %d", GameState.sharedInstance.highScore)
    addChild(lblHighScore)

Then, back in GameScene, add a gameOver property ...

var gameOver = false

Still in GameScene, add this method:

func endGame() {

  gameOver = true

  // Save high score
  GameState.sharedInstance.saveState()

  let reveal = SKTransition.fadeWithDuration(0.5)
  let endGameScene = EndGameScene(size: self.size)
  self.view!.presentScene(endGameScene, transition: reveal)
}

Be sure to call ...

endGame() 

... wherever the player runs out of lives.

And you may need to add:

if gameOver {
  return 
}

... in GameScene, where the gameplay is updated.

For more help, you should Google ray wenderlich uberjump part 2. There is good info and code in that tutorial about saving and displaying game scores.