In Cocos2d-x, the class that coordinates the overall situation of the game is abstracted into the Director class (Director). Director is the core of the entire cocos2d-x engine and the navigator of the entire game. Some common operations in the game are controlled by Director, such as OpenGL ES initialization, scene conversion, control of game pause and continuation, switching between world coordinates and GL coordinates, control of nodes, etc., as well as some game data The save call, the acquisition of screen size, etc. are all managed and controlled by the Director class.
Because Director is the general director of the game project and will often call for some control, the Director uses the single-piece design pattern, that is, there is only one director class in the entire game. Use the getInstance() method to obtain an instance of Director.
Tips: In the cocos2d-x2.x version, use the sharedDirector() method to get the director class object, and in the 3.x version use getInstance() to get it, but sharedDirector() can also be used.
The inheritance relationship of the Director class is as follows:
DisplayLinkDirector inherits CCDirector and is a director class that can be automatically refreshed. It supports three animation intervals (frame intervals) of 60/1, 1/30 and 1/15.
The main public functions of the Director class are as follows:
Function name |
return type |
describe |
getRunningScene |
scene class |
Get the currently running scene |
getAnimationInterval |
floating point |
Get the time of each frame |
setAnimationInterval |
floating point |
Set the time of each frame |
isDisplayStats |
boolean |
Returns whether to display the time of each frame in the lower left corner of the screen |
setDisplayStats |
without |
Set whether to display the time of each frame in the lower left corner of the screen |
getSecondsPerFrame |
floating point |
Get the time of each frame (in seconds) |
getOpenGLView |
GL view |
Get an OpenGL view that draws all objects |
without |
Sets the OpenGL view where all objects are drawn |
|
boolean |
Whether the director class object is paused |
|
Integer |
Get the number of frames running from the director class |
|
Projection class |
Get OpenGL projection |
|
without |
Set up OpenGL projection |
|
without |
Set up the OpenGL interface |
|
boolean |
Whether the switched scene receives clear information |
|
Node class |
Get a node object traversed after the main scene traversal |
|
without |
Sets a node object to traverse after the main scene is traversed |
|
size |
Get screen size (in points) |
|
size |
Get the screen size in pixels (in pixels) |
|
size |
Get the visible screen size |
|
vector |
Get the orientation of the visible screen |
|
vector |
Convert to OpenGL coordinate system |
|
vector |
Convert to UI coordinate system |
|
without |
run the current scene |
|
without |
Suspend the current scene and push it onto the stack |
|
without |
Pop the scene from the stack |
|
without |
Pop all scenes from the stack up to the root scene |
|
without |
Pop all scenes from the stack up to a certain level (level 0 is director, level 1 is root scene) |
|
without |
replace the current scene |
|
without |
End Game |
|
without |
Pause the game |
|
without |
resume game |
|
without |
stop animation |
|
without |
start animation |
|
without |
draw the scene |
|
without |
remove all cached data |
|
without |
Set default values based on configuration information |
|
without |
Set OpenGL defaults |
|
without |
Set whether OpenGL uses the alpha channel |
|
without |
Set whether to test OpenGL depth |
|
without |
Set the surface pixel size (different from the screen size) |
|
floating point |
Get surface pixel size |
|
scheduling class |
Get the time schedule object |
|
without |
Set the time schedule object |
|
Action management class |
Get the action management object |
|
without |
Set the action management object |
|
事件调度类 |
获取事件调度对象 |
|
无 |
设置事件调度对象 |
|
渲染器 |
返回渲染器 |
|
浮点型 |
返回控制台 |
|
浮点型 |
获取帧率 |
在新建的HelloWorld项目中,打开AppDelegate.cpp,我们可以看到如下代码:
[cpp] view plain copy
- //初始化函数
- boolAppDelegate::applicationDidFinishLaunching() {
- //获取导演对象
- auto director =Director::getInstance();
- //获取OpenGL视图
- auto glview = director->getOpenGLView();
- if(!glview) {
- glview = GLView::create("MyGame");
- //设置OpenGL视图
- director->setOpenGLView(glview);
- }
- // 设置显示每帧显示时间
- director->setDisplayStats(true);
- // 设置每帧时间
- director->setAnimationInterval(1.0/ 60);
- autoscene = HelloWorld::createScene();
- // 运行场景
- director->runWithScene(scene);
- return true;
- }
- // 游戏进入后台
- voidAppDelegate::applicationDidEnterBackground() {
- //停止动画
- Director::getInstance()->stopAnimation();
- }
- // 从后台返回游戏
- voidAppDelegate::applicationWillEnterForeground() {
- //开始动画
- Director::getInstance()->startAnimation();
- }
在HelloWorldScene.cpp中有:
[cpp] view plain copy
- //初始化
- boolHelloWorld::init()
- {
- if ( !Layer::init() )
- {
- return false;
- }
- //获取OpenGL视图可见大小
- Size visibleSize= Director::getInstance()->getVisibleSize();
- //获取OpenGL视图可见方向
- Vec2 origin =Director::getInstance()->getVisibleOrigin();
- …………………………….
- }
- //导演类结束
- voidHelloWorld::menuCloseCallback(Ref* pSender)
- {
- #if(CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM ==CC_PLATFORM_WINRT)
- MessageBox("You pressed the close button.Windows Store Apps do not implement a close button.","Alert");
- return;
- #endif
- //导演类结束
- Director::getInstance()->end();
- #if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- exit(0);
- #endif
- }
由此看来,Director不愧是整个游戏的“导演”。它在游戏中无所不在,大到整个游戏的控制,小到获取屏幕的尺寸,起着至关重要的作用。