Understanding Director

Like CCNode, CCScene and CCLayer, CCDirector is one of the basic concept of Cocos2d.
     
The following figure shows the hierarchy of Cocos2D Director, Scenes, Layers, and Sprites


     
     What is CCDirector?
  • The CCDirector is the component which takes care of going back and forth between scenes.
  • The CCDirector is a shared (singleton) object. 
  • The CCDirector knows which scene is currently active.
  • The CCDirector handles a stack of scenes to allow things like “scene calls” (pausing a Scene and putting it on hold while another enters, and then returning to the original). 
  • The CCDirector is the one who will actually change the CCScene, after a CCLayer has asked for push, replacement or end of the current scene.
  • The CCDirector is responsible for initializing OpenGL ES.

     Best practices of CCDirector
  • Use fast director:
     // must be called before any other call to the director
     [Director useFastDirector];
  • If possible try to use replaceScene instead of pushScene.pushScene is very handy, but it will put the pushed scene into memory, and memory is a precious resource in the iPhone.
  • Try to avoid a big stack of pushed scenes
     -(void) mainMenu()
     {
     // etc
     [[Director sharedDirector] pushScene: gameScene];
     }
     // stack:
     //   . game  <-- running scene
     //   . mainMenu
     
     -(void) game
     {
     [[Director sharedDirector] pushScene: gameOverScene];
     }
     // stack:
     //   . gameOver  <-- running scene
     //   . game
     //   . mainMenu
     
     -(void) showGameOver
     {
     [[Director sharedDirector] pushScene: hiScoreScene];
     }
     // stack:
     //   . scores  <-- running scene (4 pushed scenes... expensive)
     //   . gameOver
     //   . game
     //   . mainMenu