How do I invalidate a timer in one method when another method is called? Basically when tapFig
is called, I want it to send a message to moveStickFig
to invalidate the timer
-(void) moveStickFig:(NSTimer *)timer {
UIButton *stick = (UIButton *)timer.开发者_JS百科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];
}
I think you need a flag in moveStickFig
that sets to true when tapFig
is called.
-(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;
// ......
}
Note: Usually, you will set the timer to call a function repeatedly at a fixed frames per second. The timer does the job of calling it at that rate. There is no need of passing timer instance repeatedly. If that is what you want, then OK. However, if you need your game logic to be continued, you need to reset the isTimerInvalidateSet to false.
Hope this helps !
精彩评论