NSTimer Milliseconds countdown
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 = 100;
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.milisecondsCount = 100;
tmp.secondsCount -= 1;
}
NSString *timerOutput = [NSString stringWithFormat:@"%2d:%2d",
tmp.secondsCount, tmp.milisecondsCount];
timeLabel.text = timerOutput;
if ((tmp.secondsCount == 0) && tmp.milisecondsCount == 0) {
NSLog(@"Test");
tmp.lives = tmp.lives - 1;
NSString *newLivesOutput = [NSString stringWithFormat:@"%d",
tmp.lives];
livesLabel.text = newLivesOutput;
[tmp.countdownTimerGame invalidate];
tmp.countdownTimerGame = nil;
[self setTimer];
if (tmp.lives == 0) {
[self performSelector:@selector(stopped) withObject:nil];
}
No comments:
Post a Comment