How to Invalidate ALL NSTimers

Discussion in 'iOS Programming' started by simplymuzik3, Jul 17, 2009.

  1. simplymuzik3 macrumors 6502a

    Joined:
    Jan 29, 2009
    #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! :)
     
  2. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #2
    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.
     
  3. simplymuzik3 thread starter macrumors 6502a

    Joined:
    Jan 29, 2009
    #3
    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
     
  4. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    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];
    	}
    
     
  5. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #5
    @ the OP - This is definitely better than my suggestion so I would go for this approach
     
  6. simplymuzik3 thread starter macrumors 6502a

    Joined:
    Jan 29, 2009
    #6

    Thanks so much! Works like a charm :D
     

Share This Page