I want to separate the game logic and rendering into two different cycles, because I do not want fps to control the speed of the game. I tried to achieve this by creating CADisplayLink for rendering and NSTimer for game logic. But a strange thing happened:
Sometimes (1 out of 15 application launches) the game runs at very low fps (about 5-10), but the rest of the time it is completely smooth. If I remove the NSTimer logic and combine the two loops, the fps will be constantly high, but this is clearly not an acceptable solution. Therefore, it seems that sometimes two timers “delay each other” or something like that, but I do not quite understand the inner workings of runloops.
This is how I create a timer and 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 problem and how to fix it?
Or can you recommend any other solutions to separate the rendering cycle and game logic?
source share