0

I want to keep a track of how many points have been scored overall in game over as many games as the person plays. For example, if a person plays the game 25 times and scores 100 each time then their total score would be 2500. This is the method I am using to increment the score in the game:

let waitScore = SKAction.waitForDuration(1.0) //add score every second
let incrementScore = SKAction.runBlock ({
    ++self.score
    self.scoreLabel.text = "\(self.score)"}) //update score label with score
self.runAction(SKAction.repeatActionForever(SKAction.sequence([waitScore,incrementScore])))

The total score will be displayed in a different scene, so I guess I need to save the total score using NSUser defaults, load it in the game scene where the score is being counted and somehow add the loaded total score to the current score then save the total score?! I hope this makes sense.

JGrn84
  • 750
  • 1
  • 9
  • 26

2 Answers2

3
  • Define a convenience constant totalScoreKey somewhere outside a class

    let totalScoreKey = "totalScore"
    
  • In the AppDelegate class register the key totalScore, for example in the init method

      override init()
      {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        let defaultValues = [totalScoreKey: 0]
        userDefaults.registerDefaults(defaultValues)
      }
    
  • getTotalScore() reads and returns the total score.
    The method can be implemented in any class

      func getTotalScore() -> Int
      {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        return userDefaults.integerForKey(totalScoreKey)
      }
    
  • updateTotalScore() reads the total score from user defaults adds the value in self.score and writes the value back.
    The method must be implemented in the class which contains the variable score

      func updateTotalScore()
      {
        let userDefaults = NSUserDefaults.standardUserDefaults()
        var totalScore = userDefaults.integerForKey(totalScoreKey)
        totalScore += self.score
        userDefaults.setInteger(totalScore, forKey: totalScoreKey)
      }
    
vadian
  • 274,689
  • 30
  • 353
  • 361
  • I've added all the code it works perfectly in the the game scene as i simply add TotalScoreLabel.text = "\(totalScore)". However when I totalScoreKey in a different scene using getTotalScore() it gives a string rather than an Int. How can I convert the key to give me an Int so I can do something similar with a label node to show the total score in another scene? I've tried totalScoreKey.toInt() but it says no code will be executed after the return. – JGrn84 Aug 02 '15 at 12:32
  • its okay, I've fixed it by by making the totalScoreKey an Int at the top level: var totalScoreKey: Int = 0. Works perfect. Thank you so much for your help. – JGrn84 Aug 02 '15 at 13:33
0

Write:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:10 forKey:@"score"];
[defaults synchronize];

Read:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger score = [defaults integerForKey:@"score"];
Gal
  • 1,582
  • 2
  • 14
  • 30