使用swift spriteKit中的NSUserDefault在新场景中显示高分

时间:2015-06-18 16:08:33

标签: swift sprite-kit nsuserdefaults skscene

我正在使用SpriteKit制作游戏并保存玩家的高分。当游戏结束时,它会转换为不同的场景(endScene)。我无法弄清楚如何在endScene中显示高分。我获得高分的代码:

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 个答案:

答案 0 :(得分:0)

要加载高分,你需要一个int来保持高分,我不确定你要用highScoreLabel做什么,但加载它你会做var highScore = defaults.integerForKey("highScore")和你的标签你可以做highScoreLabel.text = "Score \(highScore)"

答案 1 :(得分:0)

此答案假设您正在GameScene中正确显示分数。如果是这样,那么除了GameScene文件之外,还要创建另外两个Swift文件:1)GameState.swift和2)EndGameScene.swift。

在GameState.swift中,创建一个像这样的共享实例......

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()
    }
}

在GameScene文件中,确保正确创建了标签。如果是这样,那么找到为您的播放器创建积分的代码。也许在哪里发生碰撞。然后添加GameState共享实例。它看起来像这样......

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

在您的EndGameScene中,请确保使用标签调用GameState.sharedInstance。代码看起来像这样......

    // 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)

然后,回到GameScene,添加一个gameOver属性......

var gameOver = false

仍在GameScene中,添加此方法:

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)
}

请务必致电...

endGame() 

......只要玩家的生命耗尽。

您可能需要添加:

if gameOver {
  return 
}

...在GameScene中,游戏玩法更新。

要获得更多帮助,您应该使用Google ray wenderlich uberjump第2部分。该教程中有关于保存和显示游戏分数的良好信息和代码。