Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
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. –  Confused Feb 3 '12 at 18:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.