停止 NSTimer

发布于 2024-08-11 07:12:52 字数 1344 浏览 1 评论 0原文

好的,这段代码非常基本。用户将答案输入文本框,如果等于“第一+第二”,他们就得到一分。然后,他们有 5 秒钟的时间回答下一个数学问题。如果他们这样做了,函数“doCalculation”将再次运行,他们会得到另一分。如果他们不这样做,那么“onTimer”函数就会运行,然后就会出现问题。

问题是,当用户连续正确地解决多个问题时,“doCalculation”会运行多次,然后我会同时启动多个计时器。这真的开始搞砸游戏了。

我需要停止计时器。显然使用“无效”,但我不知道在哪里。我无法在计时器开始之前使计时器无效,所以...whhhhhatt?

我不确定如何做的另一种选择是,如果每当您解决问题时,它只是将计时器设置回 5 秒而不是创建一个新计时器,该怎么办?但我如何判断计时器是否已经创建呢?我不确定最好的行动方案是什么,或者语法。想法?

多谢!

- (IBAction)doCalculation:(id)sender
{
    NSInteger numAnswer = [answer.text intValue];
    if ( numAnswer == first + second) {
        numAnswered++;
        NSString *numberAnsweredCorrectly = [[NSString alloc] initWithFormat:@"%d", numAnswered];
        numCorrectlyAnswered.text = numberAnsweredCorrectly;
        answer.text = @"";

        NSTimer *mathTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];     

        //Set the variables to two HUGE numbers, so they can't keep plugging in the same answer

        first = arc4random() % 10;
        second = arc4random() % 10;

        NSString *firstString = [[NSString alloc] initWithFormat:@"%d", first];
        NSString *secondString = [[NSString alloc] initWithFormat:@"%d", second];

        firstNumber.text = firstString;
        secondNumber.text = secondString;
    }

Okay, so, this code is pretty basic. The user inputs an answer into a textbox, if it equals "first + second" they get a point. Then, they have 5 seconds to answer the next math problem. If they do, the function "doCalculation" is run again, and they get another point. IF they don't, then the function "onTimer" is run, and shit hits the fan.

The problem is, when the user gets multiple problems in a row correct, "doCalculation" is run multiple times, and then I have multiple timers going at once. This really starts screwing up the game.

I need to stop the timers. Obviously use "Invalidate" but I don't know where. I can't invalidate the timer before it starts, so... whhhhatt?

Another option that I'm not sure how to do, what if the whenever you get the problem correct it simply sets the timer back to 5 seconds instead of creating a new one. But how can I tell if the timer has already been created? I'm not sure what the best course of action is, or the syntax. Thoughts?

Thanks a lot!

- (IBAction)doCalculation:(id)sender
{
    NSInteger numAnswer = [answer.text intValue];
    if ( numAnswer == first + second) {
        numAnswered++;
        NSString *numberAnsweredCorrectly = [[NSString alloc] initWithFormat:@"%d", numAnswered];
        numCorrectlyAnswered.text = numberAnsweredCorrectly;
        answer.text = @"";

        NSTimer *mathTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];     

        //Set the variables to two HUGE numbers, so they can't keep plugging in the same answer

        first = arc4random() % 10;
        second = arc4random() % 10;

        NSString *firstString = [[NSString alloc] initWithFormat:@"%d", first];
        NSString *secondString = [[NSString alloc] initWithFormat:@"%d", second];

        firstNumber.text = firstString;
        secondNumber.text = secondString;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假面具 2024-08-18 07:12:52

我会将 mathTimer 移到您的类标头中:

//inside your .f file:
@interface YourClassNAme : YourSuperClassesName {
    NSTimer *mathTimer
}


@property (nonatomic, retain) NSTimer *mathTimer;

//inside your .m:
@implementation YourClassNAme 
@synthesize mathTimer;

-(void) dealloc {
   //Nil out [release] the property
   self.mathTimer = nil;
   [super dealloc];
}

并更改您的方法以通过属性访问计时器:

- (IBAction)doCalculation:(id)sender
{
    NSInteger numAnswer = [answer.text intValue];
    if ( numAnswer == first + second) {
        numAnswered++;
        NSString *numberAnsweredCorrectly = [[NSString alloc] initWithFormat:@"%d", numAnswered];
        numCorrectlyAnswered.text = numberAnsweredCorrectly;
        answer.text     = @"";

        [self.mathTimer invalidate];  //invalidate the old timer if it exists
        self.mathTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];             

        //Set the variables to two HUGE numbers, so they can't keep plugging in the same answer

        first = arc4random() % 10;
        second = arc4random() % 10;

        NSString *firstString = [[NSString alloc] initWithFormat:@"%d", first];
        NSString *secondString = [[NSString alloc] initWithFormat:@"%d", second];

        firstNumber.text = firstString;
        secondNumber.text = secondString;
    }

I would move the mathTimer into your class header:

//inside your .f file:
@interface YourClassNAme : YourSuperClassesName {
    NSTimer *mathTimer
}


@property (nonatomic, retain) NSTimer *mathTimer;

//inside your .m:
@implementation YourClassNAme 
@synthesize mathTimer;

-(void) dealloc {
   //Nil out [release] the property
   self.mathTimer = nil;
   [super dealloc];
}

And change your method to access to timer through the property:

- (IBAction)doCalculation:(id)sender
{
    NSInteger numAnswer = [answer.text intValue];
    if ( numAnswer == first + second) {
        numAnswered++;
        NSString *numberAnsweredCorrectly = [[NSString alloc] initWithFormat:@"%d", numAnswered];
        numCorrectlyAnswered.text = numberAnsweredCorrectly;
        answer.text     = @"";

        [self.mathTimer invalidate];  //invalidate the old timer if it exists
        self.mathTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];             

        //Set the variables to two HUGE numbers, so they can't keep plugging in the same answer

        first = arc4random() % 10;
        second = arc4random() % 10;

        NSString *firstString = [[NSString alloc] initWithFormat:@"%d", first];
        NSString *secondString = [[NSString alloc] initWithFormat:@"%d", second];

        firstNumber.text = firstString;
        secondNumber.text = secondString;
    }
盛夏已如深秋| 2024-08-18 07:12:52

如果您正在制作游戏,则应该使用游戏循环,其中游戏在该循环中更新。时间到了之后就可以查看结果了。您只需处理 1 个连续计时器。

If you you are making a game, you should use a game loop where the game is updated in that loop. Then you can check the results after the time is up. You will only have 1 continuos timer to deal with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文