iOS - 检索并保存游戏的分数

时间:2014-01-18 13:19:26

标签: ios iphone objective-c xcode

我开发了一个小游戏,所以我管理这样的东西,当第一次app午餐时,它保存了第一个记录,然后加载并比较新记录和第一次记录。但问题是当用户点击记录时应保存新记录,但事实并非如此!并再次保存第一条记录!这是我的代码:

- (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");

    }


}

保存和加载分数(方法形式为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

2 个答案:

答案 0 :(得分:2)

看起来你应该保存scoreNumber而不是bestScoreNumber


原因:

以下代码希望保存最佳分数,因为GO.ScoreNumber&gt; 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");

}

但是在saveBestScore中,它存储了bestScoreNumber,这是之前的最高分数。

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

答案 1 :(得分:1)

- (void)saveBestScore

这个方法没有参数,但它应该。

如果您将分数保持为int,则应将其修改为如下所示:

- (void)saveBestScore:(int)bestScore

然后,在首次启动时,使用saveBestScore参数调用0,只是为了初始化它。事实上,如果你正确地编写saveBestScore方法,你根本不需要检查应用程序是否已经启动过。

- (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];
}