iOS.Lover
iOS.Lover

Reputation: 6051

iOS - Retrieving and saving scores of a game

I developed a small game , , so I manage something like this , when app lunches for the first time , it save the very first record , then after that it loads and compares the new records with the the first time record . but the problem is when user hit the record it should be saved the new record but it doesn't !!! and saves the first record again ! here is my code :

- (void)manageScores {
    NSLog(@"managed");

    NSString *tempScore = [NSString stringWithFormat:@"%@",scoreLabel.text];
    GO.scoreNumber = [tempScore integerValue];
    GO.bestScoreNumber = [tempScore integerValue];
    GO.scores.text = [NSString stringWithFormat:@"score:%d",GO.scoreNumber];
    GO.bestScore.text  = [NSString stringWithFormat:@"best:%d",GO.bestScoreNumber];


    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        // app already launched
        NSLog(@"app already launched");
        [GO loadBestScore];
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        // This is the first launch ever
        NSLog(@"first time lunch ever");
        [GO saveBestScore];
    }



    if  (GO.scoreNumber > GO.bestScoreNumber) {

        //**************THIS METHOD DOESN'T WORK WHEN USER HIT THE RECORD*********/////

        [GO saveBestScore];

        //**************//        

        GO.scores.text = [NSString stringWithFormat:@"score:%d",GO.scoreNumber];
        GO.bestScore.text  = [NSString stringWithFormat:@"best:%d",GO.scoreNumber];
        GO.shareScore.text = [NSString stringWithFormat:@"score:%d",GO.bestScoreNumber];

        NSLog(@"new record");

    }


    if   (GO.scoreNumber < GO.bestScoreNumber) {

        GO.scores.text = [NSString stringWithFormat:@"score:%d",GO.scoreNumber];
        GO.bestScore.text  = [NSString stringWithFormat:@"best:%d",GO.bestScoreNumber];
        GO.shareScore.text = [NSString stringWithFormat:@"score:%d",GO.bestScoreNumber];

        [GO loadBestScore];

        NSLog(@"NO NEW RECORD");

    }


}

saving and loading scores (methods form GameOver.h/m (GO) )

- (void)saveBestScore {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:bestScoreNumber forKey:@"highScore"];
    [defaults synchronize];
    NSLog(@"BEST SCORE SAVED:%i",bestScoreNumber);

}


- (void)loadBestScore {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    bestScoreNumber = (int)[prefs integerForKey:@"highScore"];

}

enter image description here

Upvotes: 0

Views: 130

Answers (2)

Ethan Fang
Ethan Fang

Reputation: 1269

Looks like you should save scoreNumber rather than bestScoreNumber


Reason:

The following code wants to save the best score because GO.ScoreNumber > Go.BestScoreNumber.

if  (GO.scoreNumber > GO.bestScoreNumber) {

    //**************THIS METHOD DOESN'T WORK WHEN USER HIT THE RECORD*********/////

    [GO saveBestScore];

    //**************//        

    GO.scores.text = [NSString stringWithFormat:@"score:%d",GO.scoreNumber];
    GO.bestScore.text  = [NSString stringWithFormat:@"best:%d",GO.scoreNumber];
    GO.shareScore.text = [NSString stringWithFormat:@"score:%d",GO.bestScoreNumber];

    NSLog(@"new record");

}

But in saveBestScore, it stores bestScoreNumber, which is the previous highest score number.

[defaults setInteger:bestScoreNumber forKey:@"highScore"];

Upvotes: 2

nhgrif
nhgrif

Reputation: 62052

- (void)saveBestScore

This method doesn't take an argument, but it should.

If you're keeping scores as int, you should modify it to look like this:

- (void)saveBestScore:(int)bestScore

Then, on first launch, call saveBestScore with an argument of 0, just to get it initialize. In fact, if you write saveBestScore method correctly, you don't really need to check whether the app has already launched ever.

- (void)saveBestScore:(int)bestScore {
    if (bestScore > [[[NSUserDefaults standardUserDefaults] 
        objectForKey:@"BestScore"] intValue]); {

        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber 
            numberWithInt:bestScore] forKey:@"BestScore"];
    }

- (int)loadBestScore {
    return [[[NSUserDefaults standardUserDefaults] objectForKey:@"BestScore] 
        intValue];
}

Upvotes: 1

Related Questions