2011-01-29 4 views
0

다른 방법을 호출 할 때 한 방법으로 타이머를 무효화하려면 어떻게해야합니까? tapFig가 호출 기본적으로 때, 나는 그것이 타이머목표 C : 타이머를 원격으로 무효화

-(void) moveStickFig:(NSTimer *)timer { 
    UIButton *stick = (UIButton *)timer.userInfo; 
    CGPoint oldPosition = stick.center; 
    stick.center = CGPointMake(oldPosition.x + 1 , oldPosition.y); 
    if (oldPosition.x == 900) { 
     [stick removeFromSuperview]; 
     healthCount--; 
     NSLog(@"%d", healthCount); 
     [healthBar setImage:[UIImage imageNamed:[NSString stringWithFormat:@"health%d.png",healthCount]]]; 
    } 
} 

-(void) tapFig:(id)sender { 
    UIButton *stick = (UIButton *)sender; 
    count++; 
    score.text = [NSString stringWithFormat:@"%d", count]; 
    [stick removeFromSuperview]; 
    [stick release]; 
} 

답변

1

난 당신이 tapFig가 호출 될 때 true로 설정 moveStickFig에 플래그를 필요가 있다고 생각을 무효로 moveStickFig에 메시지를 보내려고합니다.

-(void) moveStickFig:(NSTimer *)timer 
{ 
    if(isTimerInvalidateSet) 
    { 
     [ self timer:invalidate ]; 
     return; 
    } 
    // ...... 
} 

// you need to pass the same timer instance to `tapFig` that you earlier passed to `moveStickFig`. 

-(void) tapFig:(id)sender 
{ 
    isTimerInvalidateSet = true; 
    [ self moveStickFig:theTimerInstance ] ; // theTimerInstance is same as earlier you passed to `moveStickFig` 

    isTimerInvalidateSet = false; 
    // ...... 
} 

참고 : 일반적으로 타이머는 초당 고정 된 프레임으로 반복적으로 함수를 호출하도록 설정합니다. 타이머는 그 속도로 호출하는 작업을 수행합니다. 타이머 인스턴스를 반복적으로 전달할 필요가 없습니다. 그것이 당신이 원하는 것이라면 OK입니다. However, if you need your game logic to be continued, you need to reset the isTimerInvalidateSet to false. 희망이 도움이됩니다!