Search code examples
iphonetimergame-loop

NSTimer and CADisplayLink conflict


I have a CADisplayLink as my main gameloop for a game, and a NSTimer that spawns enemies every ten seconds. I use...

    -(void)togglePause{
displayLink.paused = !displayLink.paused;
if (displayLink.paused) {
    [self.view addSubview:pauseOverlay];
}else {
    [pauseOverlay removeFromSuperview];
}

...to pause the gameloop, but the timer that spawns enemies will continue to go on even after the games paused, if I destroy the timers and then make another one couldn't p just exploit the pause button and just hit it before the 10 seconds go off causing enemies never to spawn? Is there an easy solution to this?


Solution

  • Ditch the NSTimer and just use a counter that you increment each time the CADisplayLink fires. Once it reaches n spawn your enemies and zero the counter. If the user pauses, when they resume the counter will be the same as it was.