0

I would like to make a simple countdown with seconds and millisecond: SS:MM. However, i would like to stop the timer or do something when the timer reach 0:00. Currently the timer works, but it doesnt stop at 0:00. I can make the seconds stop, but not the milliseconds. What is wrong?

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

}
Tobias Lindgreen
  • 297
  • 1
  • 4
  • 11

2 Answers2

1

Well. You do

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

And right after that

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

How could it ever happen that they're both 0 if, as soon as milisecond reaches 0, you reset it to 100?

EDIT: Do instead something like:

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; 
}
micantox
  • 5,446
  • 2
  • 23
  • 27
  • But do you have some sort of solution to my problem then? I cant make it work otherwise. – Tobias Lindgreen Sep 27 '13 at 13:54
  • THANKS! It seems to work now, however, my countdown stops 1 second too early? Before the last 100 milliseconds can "run off" do you know why? – Tobias Lindgreen Sep 27 '13 at 14:27
  • It probably depends on where you update the label, i would suggest that you use 99 milliseconds anyway like in my edit. Maybe edit your question with your updated code? – micantox Sep 27 '13 at 14:36
  • Okay the code is now updated - but i cant figure out where to update the label for the last 0s and 99 ms to finish? – Tobias Lindgreen Sep 27 '13 at 14:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38189/discussion-between-micantox-and-tobias-lindgreen) – micantox Sep 27 '13 at 15:05
0

In your code, a first condition is met

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

so that the next conditional statment

 && tmp.milisecondsCount == 0

will never be true.

marsei
  • 7,691
  • 3
  • 32
  • 41