CCDirector负责创建和处理主窗口,和管理场景的的执行。同时负责:
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];
初始化流程:
1. director_ = (CCDirectorIOS*)[CCDirector sharedDirector];
[CCDirector sharedDirector]返回的对象是CCDirectorDisplayLink,创建CCScheduler(调度器)、CActionManager(动作管理器)、CCTouchDispatcher(触摸事件处理器),并把创建的动作管理器加入调度器,然后调度器就在时间片离调用CActionManager相关方法。(相关文件:CCDirector.m, CCDirectorIOS.m)
2. [director_ setAnimationInterval:1.0/60]; 设置FPS。
3. 初始化GLView,为渲染准备一个视图。
3. [director_ setView:__glView];
[director_ setDelegate:self];
Director是继承于UIViewController,设置视图和代理者。
4. CCScene *scene = [CCScene node];
创建主场景。
5 [director_ pushScene: scene];
把主场景推入场景堆栈,并执行。
查看更多相关代码:test/DirectorTest.m
2013/01/24 14:44 于上海