简介
CCDirector负责创建和处理主窗口,和管理场景的的执行。同时负责:
- 初始化OpenGL ES的context
 - 设置OpenGL像素格式(默认是RGB565)
 - 设置OpenGL缓冲深度(默认是0-bit)
 - 设置投影模式(默认是3D)
 
CCDirector一般作为单件使用,标准用法是:[[CCDirector sharedDirector] methodName]。IOS下[CCDirector sharedDirector]返回的对象是CCDirectorDisplayLink。
CCDirector继承自UIViewController。CDirector是真个引擎的核心,它控制整个运行过程。一般初始化代码如下:
1  | 
  | 
初始化流程:
- director_ = (CCDirectorIOS*)[CCDirector sharedDirector];
 
[CCDirector sharedDirector]返回的对象是CCDirectorDisplayLink,创建CCScheduler(调度器)、CActionManager(动作管理器)、CCTouchDispatcher(触摸事件处理器),并把创建的动作管理器加入调度器,然后调度器就在时间片离调用CActionManager相关方法。(相关文件:CCDirector.m, CCDirectorIOS.m)
[director_ setAnimationInterval:1.0/60]; 设置FPS。
初始化GLView,为渲染准备一个视图。
[director_ setView:__glView];
[director_ setDelegate:self];
Director是继承于UIViewController,设置视图和代理者。
- CCScene *scene = [CCScene node];
 
创建主场景。
5 [director_ pushScene: scene];
把主场景推入场景堆栈,并执行。
任务
- Memory Helper:使用purgeCacheData方法,可以自动清除所有cocos2d缓存的数据
 - Scene OpenGL Helper:可以设置OpenGL的Alpha混合和深度检测(setAlphaBlending、setDethTest)
 - Director Integration with a UIKit view:
 - Director Scene Landscape:场景布局
 - Director Scene Management:管理场景(runWithscene, pushScene, popScene, replaceScene, end, pause, resume, stopAnimation,startAnimation,drawScene)
 
属性
- runningThread:cocos2d线程
 - runningScene:当前正在运行的场景,cocos2d一次只能运行一个场景
 - animationInterval:FPS
 - displayStats:控制是否显示一些统计信息
 - isPaused:控制Director是否暂停
 - isAnimating:控制Director是否运行
 - projection:设置OpenGL的投影
 - totalFrames:从Director开始运行,执行的帧数
 - secondsPerFrame:每一帧用时
 - deltegate:实现CDirectorDelegate协议的代理对象
 - scheduler:调度器
 - actionManager:动作管理器
 - touchDispatcher:用户触摸操作的处理器
 
查看更多相关代码:
<test/DirectorTest.m>