Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

simplymuzik3

macrumors 6502a
Original poster
Jan 29, 2009
590
1
Okay, i have a problem. I need to invalidate all NSTimers in my app, even if they have not been called/used yet. Does anyone know of a way to do this? if i try [timer invalidate], it crashes because some of the timer's have not even been used yet. This is for my "game over" in my app, and since the user always loses at a different level, I need a way to disable all timers. Thanks for the help! :)
 
Okay, i have a problem. I need to invalidate all NSTimers in my app, even if they have not been called/used yet. Does anyone know of a way to do this? if i try [timer invalidate], it crashes because some of the timer's have not even been used yet. This is for my "game over" in my app, and since the user always loses at a different level, I need a way to disable all timers. Thanks for the help! :)

I'm not quite sure I understand, do you have a seperate timer for each level in the game?

If so surely you know what level the user is on currently and you could do a bit of logic based on that? If your game doesn't have a huge amount of levels a switch case would be the easiest way. You could even use an array of bools, each bool representing if a timer has been ran or not, when it comes to invalidating the timers you could then loop through the bools and if the bool is true (timer has been ran) then invalidate it.

Could you not invalidate the timer for each level before you progress to the next? That way you only have to invalidate the timer of the level the user died on when you get to game over.
 
Well not ALL levels have timers. Some levels have timers, and each timer is used to control different things (animation, moving images etc.). There are 14 timers that I need to invalidate in total, however, depending on when the user gets "game over", there is going to be some timers that never got activated. I need to find a way to disable ALL timers, regardless if they have been activated/used or not. Hope this helps, and thanks for the reply. :D
 
i would place all my timers in an array, and then stop them if they're running (if they're valid) using fast enumeration.

Code:
NSArray *timerObjectsArray = [[NSArray alloc] initWithObjects:timer1, timer2, timer3, nil];
for (id timerObject in timerObjectsArray)
	{
	if ([timerObject isValid])
		[timerObject invalidate];
	}
 
i would place all my timers in an array, and then stop them if they're running (if they're valid) using fast enumeration.

Code:
NSArray *timerObjectsArray = [[NSArray alloc] initWithObjects:timer1, timer2, timer3, nil];
for (id timerObject in timerObjectsArray)
    {
    if ([timerObject isValid])
        [timerObject invalidate];
    }

@ the OP - This is definitely better than my suggestion so I would go for this approach
 
i would place all my timers in an array, and then stop them if they're running (if they're valid) using fast enumeration.

Code:
NSArray *timerObjectsArray = [[NSArray alloc] initWithObjects:timer1, timer2, timer3, nil];
for (id timerObject in timerObjectsArray)
	{
	if ([timerObject isValid])
		[timerObject invalidate];
	}


Thanks so much! Works like a charm :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.