Director of cocos2dx study notes (director class)

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

setOpenGLView

without

Sets the OpenGL view where all objects are drawn

isPaused 

boolean

Whether the director class object is paused

getTotalFrames

Integer

Get the number of frames running from the director class

getProjection

Projection class

Get OpenGL projection

setProjection

without

Set up OpenGL projection

setViewport

without

Set up the OpenGL interface

isSendCleanupToScene

boolean

Whether the switched scene receives clear information

getNotificationNode

Node class

Get a node object traversed after the main scene traversal

setNotificationNode

without

Sets a node object to traverse after the main scene is traversed

getWinSize

size

Get screen size (in points)

getWinSizeInPixels

size

Get the screen size in pixels (in pixels)

getVisibleSize

size

Get the visible screen size

getVisibleOrigin

vector

Get the orientation of the visible screen

convertToGL

vector

Convert to OpenGL coordinate system

convertToUI

vector

Convert to UI coordinate system

runWithScene

without

run the current scene

pushScene

without

Suspend the current scene and push it onto the stack

popScene

without

Pop the scene from the stack

popToRootScene

without

Pop all scenes from the stack up to the root scene

popToSceneStackLevel 

without

Pop all scenes from the stack up to a certain level

(level 0 is director, level 1 is root scene)

replaceScene

without

replace the current scene

end

without

End Game

pause 

without

Pause the game

resume 

without

resume game

stopAnimation

without

stop animation

startAnimation

without

start animation

drawScene 

without

draw the scene

purgeCachedData 

without

remove all cached data

setDefaultValues

without

Set default values ​​based on configuration information

setGLDefaultValues

without

Set OpenGL defaults

setAlphaBlending

without

Set whether OpenGL uses the alpha channel

setDepthTest 

without

Set whether to test OpenGL depth

setContentScaleFactor

without

Set the surface pixel size (different from the screen size)

getContentScaleFactor

floating point

Get surface pixel size

getScheduler

scheduling class

Get the time schedule object

setScheduler

without

Set the time schedule object

getActionManager

Action management class

Get the action management object

setActionManager

without

Set the action management object

getEventDispatcher

事件调度类

获取事件调度对象

setEventDispatcher

设置事件调度对象

getRenderer 

渲染器

返回渲染器

getDeltaTime

浮点型

返回控制台

getFrameRate

浮点型

获取帧率

在新建的HelloWorld项目中,打开AppDelegate.cpp,我们可以看到如下代码:

 

[cpp] view plain copy

  1. //初始化函数  
  2. boolAppDelegate::applicationDidFinishLaunching() {  
  3. //获取导演对象  
  4. auto director =Director::getInstance();  
  5. //获取OpenGL视图  
  6.     auto glview = director->getOpenGLView();  
  7.     if(!glview) {  
  8.         glview = GLView::create("MyGame");  
  9.             //设置OpenGL视图  
  10.         director->setOpenGLView(glview);  
  11.     }  
  12.     // 设置显示每帧显示时间  
  13.     director->setDisplayStats(true);  
  14. // 设置每帧时间  
  15. director->setAnimationInterval(1.0/ 60);  
  16.     autoscene = HelloWorld::createScene();  
  17.     // 运行场景  
  18.     director->runWithScene(scene);  
  19.     return true;  
  20. }  
  21. // 游戏进入后台  
  22. voidAppDelegate::applicationDidEnterBackground() {  
  23.      //停止动画  
  24.     Director::getInstance()->stopAnimation();  
  25. }  
  26. // 从后台返回游戏  
  27. voidAppDelegate::applicationWillEnterForeground() {  
  28.      //开始动画  
  29.     Director::getInstance()->startAnimation();  
  30. }  

在HelloWorldScene.cpp中有:

 

[cpp] view plain copy

  1. //初始化  
  2. boolHelloWorld::init()  
  3. {  
  4.     if ( !Layer::init() )  
  5.     {  
  6.         return false;  
  7.     }  
  8.     //获取OpenGL视图可见大小  
  9. Size visibleSize= Director::getInstance()->getVisibleSize();  
  10. //获取OpenGL视图可见方向  
  11. Vec2 origin =Director::getInstance()->getVisibleOrigin();  
  12. …………………………….  
  13. }  
  14. //导演类结束  
  15. voidHelloWorld::menuCloseCallback(Ref* pSender)  
  16. {  
  17. #if(CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM ==CC_PLATFORM_WINRT)  
  18.      MessageBox("You pressed the close button.Windows Store Apps do not implement a close button.","Alert");  
  19.     return;  
  20. #endif  
  21.      //导演类结束  
  22.     Director::getInstance()->end();  
  23. #if(CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  24.     exit(0);  
  25. #endif  
  26. }  

由此看来,Director不愧是整个游戏的“导演”。它在游戏中无所不在,大到整个游戏的控制,小到获取屏幕的尺寸,起着至关重要的作用。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325823684&siteId=291194637