2013-09-27 3 views
0

초 및 밀리 초 : SS : MM으로 간단한 카운트 다운을하고 싶습니다. 그러나 타이머를 중지하거나 타이머가 0:00에 도달하면 작업을 수행하려고합니다. 현재 타이머가 작동하지만 0시에 멈 춥니 다. 초를 멈출 수는 있지만 밀리 초가되지는 않습니다. 뭐가 잘못 되었 니?NSTimer 밀리 초 카운트 다운

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

} 
+0

여기 좀 도와주세요 : http://stackoverflow.com/questions/15311289/nstimer-creating-a-timer-countdown – Woodstock

+0

누가 이런 질문을 upvote 이해할 수 있습니까? – micantox

답변

1

우물. 당신은

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

그리고 바로 그

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

어떻게 이제까지 그들이 모두 0, 경우 즉시있어 milisecond0에 도달, 당신은 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; 
} 
+0

하지만 내 문제에 대한 해결책이 있습니까? 나는 그렇지 않으면 작동하지 않는다. –

+0

좋아요, 제 질문을 수정했습니다.보세요 – micantox

+0

고맙습니다! 그러나 지금은 작동하는 것 같습니다. 카운트 다운이 1 초에 너무 일찍 끝나나요? 지난 100 밀리 초가 "도망 갈"수 있기 전에 그 이유를 아십니까? –

0

, 제 조건은 다음 조건 한 Statment는

&& tmp.milisecondsCount == 0 

가 없을 것 그래서

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

을 충족 참된.