I want to separate the game logic and the rendering into two different loops, because I don't want the fps to control the game speed. I tried to achieve this by creating a CADisplayLink for the rendering, and an NSTimer for the game logic. But then a strange thing happened:
Sometimes (1 out of 15 application runs) the game runs on a very low fps (about 5-10), but the rest of the times it's completely smooth. If I remove the game logic's NSTimer and combine the two loops the fps is consistently high, but it's obviously not an acceptable solution. So it looks like that sometimes the two timers 'delaying each other' or something like that, but I don't completely understand the inner working of runloops.
Here's how I create the timer and the displaylink:
NSTimer *gameTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:1.0 / 60.0 target:self selector:@selector(gameTimerFired:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:gameTimer forMode:NSDefaultRunLoopMode];
[gameTimer release];
CADisplayLink *aDisplayLink = [[UIScreen mainScreen] displayLinkWithTarget:self selector:@selector(drawFrame)];
[aDisplayLink setFrameInterval:animationFrameInterval];
[aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
self.displayLink = aDisplayLink;
Can you tell me what causes the fps issue and how to fix it?
Or can you recommend any other solutions to separate the rendering and the game logic loop?