2011-01-29 6 views
0

別のメソッドが呼び出されたときに、あるメソッドでタイマーを無効にするにはどうすればよいですか?基本的にはtapFigが呼び出されたとき、私は私はあなたがtapFigが呼び出されたときにtrueに設定しますmoveStickFig内のフラグが必要だと思う、それは目的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

タイマーを無効にする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; 
    // ...... 
} 

注:通常、関数を1秒間に固定フレームで呼び出すようにタイマーを設定します。タイマーは、そのレートで呼び出すという仕事をします。タイマーインスタンスを繰り返し渡す必要はありません。それがあなたが望むものなら、OK。 However, if you need your game logic to be continued, you need to reset the isTimerInvalidateSet to false.これが役立つことを願っています!

関連する問題