views:

526

answers:

2

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;
    }
A: 

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.

willi
+3  A: 

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;
    }
Brad Smith