Release NSObject and stop method call
So my menu is calling the game with this piece of code:
game = [[Game alloc] init];
[self presentModalViewController:memoryTest animated:FALSE];
Then the UIViewController appears with a countdown. The player can return to the menu DURING the countdown. However, when I try to do this, the countdown keeps running and eventually the game starts, it was even thought that the UIViewController was fired (so the UIView disappeared) in the backToMenu method.
[self.parentViewController dismissModalViewControllerAnimated:FALSE];
I tried to release the game object in the viewDidAppear method in the menu, but no luck. I was thinking that I have a "throw" BOOL value, so the countdown can be checked or not, the player is out of the game, but there must be a better way to free the object AND stop all method calls inside it.
Thanks for helping me.
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 source to share
I assumed you would be using something like NSTimer. It might not actually be a bad idea. NSTimer can handle restarting the countdown method every 0.8 seconds. If you decrease the value to 0, your countdown has expired. If your view disappears, disable the timer in viewWillDisappear.
a source to share