Could anyone theoretically explain how the game loop works in Cocos2D and Objective-C?

If you need me to be more specific about what I'm asking, please read on.

I've got a rudimentary understanding of C, pointers, arrays, conditionals, loops, strings, variables, methods, structs etc... procedural thinking makes sense to me.

Cocos2D tutorials tend to introduce Director, Scenes, Layers, Nodes and Actions, then just dive into writing classes and away they go.

I'm missing something: I'm yet to grasp how the Cocos2D game loop operates - I don't know how Cocos2D decides it's time to go render the screen again then wait again for input/events again each frame.

And if there's a lot going on in the classes, how does this game loop interrupt them to say, "sorry guys, gotta render another frame, hold up a bit". Or similar.

What, when, how and where is Cocos2D instigating each "tick" of the game engine to render etc, and how does a game start itself into this game loop environment - and therefore how does one decide to structure classes and messaging activity within this frame work for any given game design?

If you're looking for an example to explain this, please use the original Asteroids game.


1 Answer 1


On iOS 5 Cocos2d uses the CADisplayLink (Apple's doc).

It' a timer object that allows synchronizing your application's drawing to the refresh rate of the display.

Cocos2d has a class which receives all dispatches from CADisplayLink. It calls drawScene on the director class which then calls the scheduler's tics method (which notifies the timers which need to be notified) then it does scene transitions and finally it calls the draw code of the root CCNode.

  • \$\begingroup\$ Is there somewhere I can get a grasp on how all this works theoretically, like a gentle overview, so that I can then THINK my way through what it's doing? Any of the Cocos2D books go through this theoretically with a minimum assumption of what is going on? It's still very dark for me. I sort of see what you're saying, but it doesn't come back down to my low level of knowledge. \$\endgroup\$
    – Confused
    Commented Feb 3, 2012 at 18:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .