Save and load highscores

I tried using NSUserDefults to save and load the recorder (nsinteger) for my game. I created a void function that checks if there is more gameplay than my record and if it switches between them. I want every time the game is over, a shortcut will appear that shows my personal record. For this, I create a property for my NSUsweDefults, in my viewdidload I tried to load currect highscore and another function (checkIfHighscore). here is my NSUserDefults property:

@property(readwrite) NSUserDefaults *prefs; 

here is my viewdidload code:

  NSInteger currectHighscore = [prefs integerForKey:@"highscore"]; highScoreLabel.text = [NSString stringWithFormat:@"%d",currectHighscore]; 

here is my checkIfHighscore:

 -(void)checkIfHighScore { if(gameOverScore > highScore) { highScore = gameOverScore; [self newHighscoreAnimation]; [prefs setInteger:highScore forKey:@"highscore"]; [prefs synchronize]; } highScoreLabel.text = [NSString stringWithFormat:@"Highscore: %d", highScore]; } 

when I enter this view manager my highscorelabel shows 0 as if it does not save my recorder.

what am I doing wrong? thanks!

+4
source share
1 answer
 // Snippet used to save your highscore in the prefs. int highScore = yourGameScore; [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"]; [[NSUserDefaults standardUserDefaults] synchronize]; // Snippet used to get your highscore from the prefs. highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] intValue ]; 
+3
source

All Articles