title: How to add coin score so player accumulates more and more over time, swift
body: <p>I created a score and high score label. The score gets 1 pint each time the player hits a coin, the high score gets updated every time the score is higher then the high score. Now, besides this I want to change the function of the score. I don't want it to start from 0 each time the game restarts, I want it to start at the last amount the player won during the previous game. So pretty much just a score label that keeps accumulating more and more points over time even when the player shuts down the app.</p>
  
  <p>This is my current code with the normal score that starts from 0 each time the player starts the game and a highscore that gets updated each time the score reaches a higher amount then the high score.</p>
  
  <pre><code>let highScoreLabel = SKLabelNode()
  
  var score: Int {
  
      switch (self) {
  
      case coin: return 1
  
      default: return 0
  
      }
  }
  
  func playerScoreUpdate() {
      playerScorelabel.text = "Score: \(playerScore)"
  }
  
  func saveHighScore(high:Int) {
      NSUserDefaults.standardUserDefaults().setInteger(high, forKey: "highscore")
  }
  func highScore() -&gt; Int {
      return NSUserDefaults.standardUserDefaults().integerForKey("highscore")
  }
  func resetHighScore() {
      NSUserDefaults.standardUserDefaults().removeObjectForKey("highscore")
  }
  
  
  func didBeginContact(contact: SKPhysicsContact) {
      var firstBody = SKPhysicsBody()
      var secondBody = SKPhysicsBody()
  
      if contact.bodyA.categoryBitMask &lt; contact.bodyB.categoryBitMask {
          firstBody = contact.bodyA
          secondBody = contact.bodyB
      } else {
          firstBody = contact.bodyB
          secondBody = contact.bodyA
      }
  
  
      if (firstBody.categoryBitMask &amp; UInt32(shipCategory)) != 0 &amp;&amp; (secondBody.categoryBitMask &amp; UInt32(obstacleCategory)) != 0 {
          ship.removeFromParent()
          let reveal = SKTransition.flipHorizontalWithDuration(0.5)
          let scene = GameOverScene(size: self.size)
          self.view?.presentScene(scene, transition: reveal)
      }
  
      if (firstBody.categoryBitMask &amp; UInt32(shipCategory)) != 0 &amp;&amp; (secondBody.categoryBitMask &amp; UInt32(coinCategory)) != 0 {
          secondBody.node?.removeFromParent() // Changed line.
          playerScore = playerScore + 1
          playerScoreUpdate()
      }
  
      if (firstBody.categoryBitMask &amp; UInt32(shipCategory)) != 0 &amp;&amp; (secondBody.categoryBitMask &amp; UInt32(diamondCategory)) != 0 {
          secondBody.node?.removeFromParent() // Changed line.
      }
  
      //CHANGE TO YOU WON SCENE
      //CHECK TO SEE IF COINS ARE 10, THEN YOU WON
      if playerScore == 30 {
  
          let reveal = SKTransition.flipHorizontalWithDuration(0.5)
          let scene = GameWonScene(size: self.size)
          self.view?.presentScene(scene, transition: reveal)
  
      }
  
      if playerScore &gt; highScore() {
          saveHighScore(playerScore)
          println("New Highscore = " + highScore().description)
          highScoreLabel.text = "HighScore: \(highScore().description)"
  
      } else {
          println("HighScore = " + highScore().description )  // "HighScore = 100"
  
      }
  }
  </code></pre>