如何使用NSUserDefault保存最佳时间分数

时间:2014-03-04 15:50:03

标签: ios objective-c

嘿伙计们,我对编程非常陌生,尤其是使用Objective C,所以请耐心等待。我一直在练习我正在研究的iPhone应用程序项目的技能,现在我遇到了节省时间的问题。我有一个简单的滑块益智游戏,有一个计时器,直到你完成拼图。时间以“00:00”格式表示。我希望能够在完成后保存最短时间,并在应用程序启动时将其显示在屏幕上,以供参考。然而,我面临的问题是它始终将时间0保存为最低值,没有什么可以击败它。

以下是我的代码:

定时器

-(void)setTimer
{
    self.secondsCount = 0;
    self.gameCountDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
}

-(void)timerRun
{
    self.secondsCount++;

    NSString *timerOutput = [NSString stringWithFormat:@" %02.f : %02i", round(self.secondsCount / 60), self.secondsCount % 60 ];

    self.timeLabel.text = timerOutput;
}

StartGame按钮

- (IBAction)start:(id)sender {
    // reset steps

    self.secondsCount=0;
    step = 0;
    [self setTimer];

    // set the view so that it can interact with touches
    [board setUserInteractionEnabled:YES];

    // set the label text to 'reset'
    [startButton setTitle:@"Reset" forState:UIControlStateNormal];

    // initialize the board, lets play!
    [board playWithImage:image andSize:(boardSize.selectedSegmentIndex+3)];
}

游戏结束

 (void)puzzleBoardDidFinished:(PuzzleGameView *)puzzleBoard {

    // add the full image with 0.0 alpha in view
    if(self.secondsCount!=0){
        self.finalTime = self.secondsCount;
        NSLog(@"seconds is: %i",self.secondsCount);
        [self saveTime];

        if(self.finalTime<self.bestTime) {

            NSLog(@"bestTime! is: %i",self.finalTime);
        [self saveTime];
        }
    }
    [self.gameCountDownTimer invalidate];
    self.gameCountDownTimer = nil;

    UIImageView *fullImage = [[UIImageView alloc]initWithImage:image];
    fullImage.frame = board.bounds;
    fullImage.alpha = 0.0;
    [board addSubview:fullImage];

    [UIView animateWithDuration:.4 
                     animations:^{
                         // set the alpha of full image to 1.0

                         fullImage.alpha = 1.0;                    
                     } 
                     completion:^(BOOL finished){
                         // set the view interaction and set the label text

                         NSLog(@"Congrats! You finish this %d x %d puzzle with %d steps", (boardSize.selectedSegmentIndex+3), (boardSize.selectedSegmentIndex+3), step);
                         [board setUserInteractionEnabled:NO];
                         [startButton setTitle:@"Start" forState:UIControlStateNormal];
                     }];    
}

并尝试保存数据

-(void)saveTime {
    [[NSUserDefaults standardUserDefaults]setInteger:self.secondsCount forKey:@"BestTime"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

这是我的viewdidload

self.bestTime = [[NSUserDefaults standardUserDefaults]integerForKey:@"BestTime"];
    self.bestTimeLabel.text = [NSString stringWithFormat:@" %02.f : %02i", round(self.bestTime / 60), self.bestTime % 60 ];

如前所述,我很新,所以任何建议和建议都会很棒。我非常渴望了解更多有关这些内容的信息,如果你们能帮助我们,我将非常感激。谢谢

2 个答案:

答案 0 :(得分:0)

您的代码看起来不错。我很确定这只是显示NSLog中的值不起作用。

变化:

self.bestTimeLabel.text = [NSString stringWithFormat:@" %02.f : %02i", round(self.bestTime / 60), self.bestTime % 60 ];

要:

self.bestTimeLabel.text = [NSString stringWithFormat:@" %02.f : %02i", round(self.bestTime / 60.0f), self.bestTime % 60 ];

对于float变量:int / int产生整数,您将其分配给float并将其打印为0.00

答案 1 :(得分:0)

每次将[[NSUserDefaults standardUserDefaults]integerForKey:@"BestTime"]的值分配给self.bestTime时,if(self.bestTime == 0){ self.bestTime = finalTime; }似乎为0。

此代码可能有所帮助:

{{1}}