繁体   English   中英

目标C:远程使计时器无效

[英]Objective C: Remotely invalidate a timer

如何在调用另一个方法时使一个方法中的计时器无效? 基本上,当tapFig时,我希望它发送一条消息到moveStickFig以使计时器无效

-(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];
}

我认为你需要在一个标志moveStickFig ,设置为true时tapFig被调用。

-(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;
    // ......
}

注意:通常,您将设置计时器以每秒固定帧的速度重复调用函数。 计时器以该速率进行调用。 无需重复传递计时器实例。 如果那是您想要的,那么确定。 However, if you need your game logic to be continued, you need to reset the isTimerInvalidateSet to false. 希望这可以帮助 !

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM