CCDirector負責建立和處理主窗口,和管理場景的的執行。同時負責: node
CCDirector通常做爲單件使用,標準用法是:[[CCDirector sharedDirector] methodName]。IOS下[CCDirector sharedDirector]返回的對象是CCDirectorDisplayLink。 緩存
CCDirector繼承自UIViewController。CDirector是真個引擎的核心,它控制整個運行過程。通常初始化代碼以下: 佈局
// Main Window window_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Director director_ = (CCDirectorIOS*)[CCDirector sharedDirector]; [director_ setDisplayStats:NO]; [director_ setAnimationInterval:1.0/60]; // GL View CCGLView *__glView = [CCGLView viewWithFrame:[window_ bounds] pixelFormat:kEAGLColorFormatRGB565 depthFormat:0 /* GL_DEPTH_COMPONENT24_OES */ preserveBackbuffer:NO sharegroup:nil multiSampling:NO numberOfSamples:0 ]; [director_ setView:__glView]; [director_ setDelegate:self]; director_.wantsFullScreenLayout = YES; // Retina Display ? [director_ enableRetinaDisplay:useRetinaDisplay_]; // Navigation Controller navController_ = [[UINavigationController alloc] initWithRootViewController:director_]; navController_.navigationBarHidden = YES; // AddSubView doesn't work on iOS6 [window_ addSubview:navController_.view]; // [window_ setRootViewController:navController_]; [window_ makeKeyAndVisible]; // create the main scene CCScene *scene = [CCScene node]; .... // and run it! [director_ pushScene: scene];
初始化流程: spa
1. director_ = (CCDirectorIOS*)[CCDirector sharedDirector]; 線程
[CCDirector sharedDirector]返回的對象是CCDirectorDisplayLink,建立CCScheduler(調度器)、CActionManager(動做管理器)、CCTouchDispatcher(觸摸事件處理器),並把建立的動做管理器加入調度器,而後調度器就在時間片離調用CActionManager相關方法。(相關文件:CCDirector.m, CCDirectorIOS.m) 代理
2. [director_ setAnimationInterval:1.0/60]; 設置FPS。 code
3. 初始化GLView,爲渲染準備一個視圖。 orm
3. [director_ setView:__glView]; 對象
[director_ setDelegate:self]; 繼承
Director是繼承於UIViewController,設置視圖和代理者。
4. CCScene *scene = [CCScene node];
建立主場景。
5 [director_ pushScene: scene];
把主場景推入場景堆棧,並執行。
查看更多相關代碼:test/DirectorTest.m