Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Richard Rodriguez
2,687 PointsNSUserDefaults was saving my scores correctly, but now every time a player scores zero, it shows the previous save?Help!
This only happens for the value zero, every other number saves correctly and displays correctly, I would appreciate any tips !!!
Richard Rodriguez
2,687 Points-(void) addPoints: (NSInteger)points { self.score += points;
SKLabelNode *scoreLabel = (SKLabelNode*) [self childNodeWithName:@"score"];
scoreLabel.text = [NSString stringWithFormat:@"%ld", self.score];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:self.score forKey:@"score"];
self.highscorenumber= 0;
self.highscorenumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
if (self.score > self.highscorenumber) {
[[NSUserDefaults standardUserDefaults] setInteger:self.score forKey:@"HighScoreSaved"];
}
Richard Rodriguez
2,687 PointsSKLabelNode *score = [SKLabelNode labelNodeWithFontNamed:@"MarkerFelt-Wide"]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSInteger *retrievedScore = [defaults integerForKey:@"score"]; score.text = [NSString stringWithFormat:@"Score: %ld", retrievedScore]; score.position = CGPointMake(CGRectGetMidX(self.frame), 225); [self addChild:score];
This is how i pull the value in my gameoverscene
2 Answers
Stone Preston
42,016 Pointsmay or may not be the problem but one thing you need to do is check to make sure the high score actually exists after pulling it from the defaults. you can remove the line that sets it to 0 since it will either have a value or be nil after setting it to the value in the defaults.
self.highscorenumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
if (self.highscorenumber) {
if (self.score > self.highscorenumber) {
[[NSUserDefaults standardUserDefaults] setInteger:self.score forKey:@"HighScoreSaved"];
}
} else {
// highscore was not in defaults
}
Richard Rodriguez
2,687 PointsThe highscore exists and works fine as well. The only problem is when the player gets a zero it just won't recognize it and it will assign the score label with the previous save. The only time it displays zero is when I run the application from scratch like if one was to install the application for the first time.
Stone Preston
42,016 Pointsok so its the actual score label thats not working, it does not have anything to do with highscore?
Richard Rodriguez
2,687 PointsStone Preston Is there a way to reset the NSUserDefaults for a specific key? If I could reset the "score" key every time I presented the GamePlayScene then it would work !
Richard Rodriguez
2,687 PointsI just changed what you told me to change, but the high score label and score label are working fine. The only time is when the score is zero, the score label won't recognize the zero and shows the previous save. This is kind of frustrating because it was working fine
Stone Preston
42,016 PointsStone Preston
42,016 Pointscan you post the code you are using to display/pull the scores from defaults