object - iphone multiple NSTimer troubles -


I am still new to programming so please forgive me if it is silly I am doing a simple game programming and different - I need multiple timers to send different messages at different intervals, so when creating a game, the following is said:

  [self gameTimerValidate]; [Self score timer evaluation]; - (zero) GameTime Validate {GameTimer = [NSTimer Scheduled Timer With Time Interval: [myGame gIntervalSpeed] Goal: Self Selector: @selector (gameTimerInterval :) userInfo: Zero repeats: Yes]; } - (zero) ScoretimeWillidate {Score Timer = [NSTimer Scheduled Timer with Time Interface: 0.02 Target: Self Selector: @Selector (ScoreTime Interface :) User Information: Zero Repeats: Yes]; }  

I have the timer and gaming timer declared in my title file ("NSTimer * gameTimer;"). I invalidate the timer while pausing the game or completing the level, and call the methods above again when starting the game or at the next level.

I am trying to think of today why the game is stopping. The application will crash. After debugging some, I noticed that the number of gametimer was 0, and it was 2 for the scoreeter. Of course, I can not invalidate a 0 timer, but I'm not sure how it happened. / P>

Is there any specific way that I should start two different NStimers? I was not searching for it for a few hours ...

NSTimer is a difficult class It does not behave as you expect.

First of all, examples of timers are not finally kept by those items which they start but IIRC, by NSRon Loop. This means that if you have an object that creates a timer, the timers will remain active, even if you destroy the object that it creates and all other references in your custom code. The timer will continue with firing messages and you have no clue from which they are coming

Second, you can not stop / stop a timer and start again. When you invalidate it, it's dead.

I suggest creating a lighter class that will manage the timer for you so that you do not have to keep an eye on it in your other code. Eg

@interface SDL_SimpleTimerController: NSObject {NSTimer * currentTimer; NSTimeInterval theInterval; ID theTargetObj; SEL The Selector; Bull timer warning; } @property (nontomic, raten) nstimer * current timer; @ Property NSTime interval interval; @property (nonatomic, retention) id theTargetObj; @ Property seal the selector; @ Property Bowl Insercing Timer; - (SDL_SimpleTimerController *) initWithInterval: (NSTimeInterval) An interval for the target: (id) aTargetObj and selected: (SEL) selected one; - (zero) startTimer; - (zero) stop timer; Synchronize @end @implementation SDL_SimpleTimerController @ currentTimer; @ Synthesis interval; Synchronize @TargetObj; Select @Selector; @Syntsize timerIserning; - (SDL_SimpleTimerController *) initWithInterval: (NSTimeInterval) An interval for the target: (id) aTargetObj and selected: (SEL) A selector {self = [super init]; TheInterval = aninterval; TheTargetObj = aTargetObj; TheSelector = aSelector; TimerIsRunning = no; Self return; } // end initWithInterval: - (zero) startTimer {if (currentTimer) {currentTimer = zero; } CurrentTimer = [NSTimer scheduled timer with interval: interval target: theTargetObj selector: selector user information: zero repeats: yes]; TimerIsRunning = yes; } // and start timer - (zero) stop timer {if (currentTimer) {[currentTimer invalid]; CurrentTimer = zero; } TimerIsRunning = No; } // End stop timer - (zero) Delok {if (current timer) {[currentTimer release]; CurrentTimer = zero; } [TheTargetObj release]; TheTargetObj = zero; [Super DeLoc]; }

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -