tags:

views:

201

answers:

3

So my menu calls a game with this piece of code:

game = [[Game alloc] init];
[self presentModalViewController:memoryTest animated:FALSE];

A UIViewController then appears with a countdown. The player can go back to the menu DURING the countdown. However when I try this, the countdown keeps running and eventually the game starts, even thought the UIViewController has been dismissed (therefore the UIView has disappeared) in the backToMenu method.

[self.parentViewController dismissModalViewControllerAnimated:FALSE];

I've tried to release the game object in the viewDidAppear method of the menu, but no luck. I thought of having a "quit" BOOL value, so the countdown can check wether or not the player has quit the game, but there must be a better way to release an object AND stop all method calls inside it.

Thanks for helping me out.

CountDown method:

- (void)countDown {

    SoundEffect *sound = [[SoundEffect alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tick" ofType: @"wav"]];
    [sound playAndRelease];

    if(self.countDownStep > 0) {
     feedback.image = [UIImage imageNamed:[NSString stringWithFormat:@"countdown%d.png",self.countDownStep]];
     feedback.hidden = FALSE;

     [self performSelector:@selector(countDown) withObject:nil afterDelay:0.8];
    }

    else {
     [self displayMessage:self.startMessage];
     [self.game performSelector:@selector(start) withObject:nil afterDelay:0.8];
     [self performSelector:@selector(hide) withObject:nil afterDelay:0.8];
    }

    self.countDownStep--;

}
A: 

How do you handle the countdown? It seems like you want to explicitly void that countdown in the viewWillDisappear method for the UIViewController you mentioned.

JimG
By void I guess you mean "stop it"? How would you do that, other than with a variable comparison?
Sam V
A: 

I was assuming you would use something like NSTimer. In fact, that might still not be a bad idea. NSTimer can handle re-running your countdown method every 0.8 seconds. If you decrement the value to 0, your countdown has expired. If your view disappears, invalidate the timer in viewWillDisappear.

JimG
A: 

In the viewcontroller's viewWillDisappear, check if the timer is running. If it is, then just invalidate it.

[timerObject invalidate] will stop the timer

lostInTransit