0

我想用秒和毫秒做一个简单的倒计时:SS:MM。但是,我想在计时器到达 0:00 时停止计时器或做一些事情。目前计时器工作,但它不会在 0:00 停止。我可以让秒停止,但不能让毫秒停止。怎么了?

-(void) setTimer {
    MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
    tmp.milisecondsCount = 99;
    tmp.secondsCount = 2;



    tmp.countdownTimerGame = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];


}

-(void) timerRun {
    MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
    tmp.milisecondsCount = tmp.milisecondsCount - 1;



    if(tmp.milisecondsCount == 0){
        tmp.secondsCount -= 1;

        if (tmp.secondsCount == 0){

            //Stuff for when the timer reaches 0
            //Also, are you sure you want to do [self setTimer] again
            //before checking if there are any lives left?

            [tmp.countdownTimerGame invalidate];
            tmp.countdownTimerGame = nil;
            tmp.lives = tmp.lives - 1;
            NSString *newLivesOutput = [NSString stringWithFormat:@"%d", tmp.lives];
            livesLabel.text = newLivesOutput;
            if (tmp.lives == 0) {
                [self performSelector:@selector(stopped) withObject:nil];

            }
            else {[self setTimer]; }
        }
        else

            tmp.milisecondsCount = 99;
    }


    NSString *timerOutput = [NSString stringWithFormat:@"%2d:%2d", tmp.secondsCount, tmp.milisecondsCount];

    timeLabel.text = timerOutput;






}



-(void) stopped {
    NSLog(@"Stopped game");
    timeLabel.text = @"0:00";

}
4

2 回答 2

1

出色地。你做

tmp.milisecondsCount = tmp.milisecondsCount - 1;
if(tmp.milisecondsCount == 0){
    tmp.milisecondsCount = 100;
    tmp.secondsCount -= 1;
}

在那之后

if ((tmp.secondsCount == 0) && tmp.milisecondsCount == 0) {
   //stuff
}

如果一旦milisecond达到0,您将其重置为 ,它们怎么可能都为 0 100

编辑:改为:

if(tmp.milisecondsCount < 0){
    tmp.secondsCount -= 1;
    if (tmp.secondsCount == 0){
        //Stuff for when the timer reaches 0
        //Also, are you sure you want to do [self setTimer] again
        //before checking if there are any lives left?
    }
    else
        tmp.milisecondsCount = 99; 
}
于 2013-09-27T12:44:11.707 回答
0

在您的代码中,满足第一个条件

 if(tmp.milisecondsCount == 0){
     tmp.milisecondsCount = 100;

这样下一个条件语句

 && tmp.milisecondsCount == 0

永远不会是真的。

于 2013-09-27T12:45:13.157 回答