停止 NSTimer
好的,这段代码非常基本。用户将答案输入文本框,如果等于“第一+第二”,他们就得到一分。然后,他们有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会将 mathTimer 移到您的类标头中:
并更改您的方法以通过属性访问计时器:
I would move the mathTimer into your class header:
And change your method to access to timer through the property:
如果您正在制作游戏,则应该使用游戏循环,其中游戏在该循环中更新。时间到了之后就可以查看结果了。您只需处理 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.