cocos2dx学习笔记之Director(导演类)

在Cocos2d-x中,把统筹游戏大局的类抽象为导演类(Director),Director是整个cocos2d-x引擎的核心,是整个游戏的导航仪。游戏中的一些经常使用操做就是由Director来控制的,好比OpenGL ES的初始化,场景的转换,游戏暂停继续的控制,世界坐标和GL坐标之间的切换,对节点的控制等,还有一些游戏数据的保存调用,屏幕尺寸的获取等都要由Director类来管理控制的。html

由于Director是游戏项目的总导演,会常常调用进行一些控制,因此该Director利用了单件设计模式,也就是整个游戏里只有一个导演类。用getInstance() 方法取得Director的实例。设计模式

 

Tips:在cocos2d-x2.x的版本中使用sharedDirector()方法来获取导演类对象,而在3.x的版本中使用getInstance()来获取,不过sharedDirector()也能够使用。缓存

Director类的继承关系以下:app

DisplayLinkDirector继承CCDirector,是一个能够自动刷新的导演类。它支持60/一、1/30和1/15三种动画间隔(帧间隔)。函数

Director类的主要公共函数以下:测试

函数名动画

返回类型spa

描述.net

getRunningScene设计

场景类

获取当前正在运行的场景

getAnimationInterval

浮点型

获取每帧的时间

setAnimationInterval

浮点型

设置每帧的时间

isDisplayStats

布尔型

返回是否在屏幕左下角显示每帧的时间

setDisplayStats

设置是否在屏幕左下角显示每帧的时间

getSecondsPerFrame

浮点型

获取每帧的时间(单位为秒)

getOpenGLView

GL视图

获取绘制全部对象的OpenGL视图

setOpenGLView

设置绘制全部对象的OpenGL视图

isPaused 

布尔型

导演类对象是否暂停

getTotalFrames

整型

获取从导演类开始运行的帧数

getProjection

投影类

获取OpenGL投影

setProjection

设置OpenGL投影

setViewport

设置OpenGL接口

isSendCleanupToScene

布尔型

切换的场景是否接收清除信息

getNotificationNode

节点类

获取一个在主场景遍历后遍历的节点对象

setNotificationNode

设置一个在主场景遍历后遍历的节点对象

getWinSize

尺寸

获取屏幕大小(单位为点)

getWinSizeInPixels

尺寸

获取像素级的屏幕大小(单位为像素)

getVisibleSize

尺寸

获取可见屏幕大小

getVisibleOrigin

矢量

获取可见屏幕的方向

convertToGL

矢量

转化为OpenGL坐标系

convertToUI

矢量

转化为UI坐标系

runWithScene

运行当前场景

pushScene

挂起当前场景,压入栈中

popScene

从栈中弹出场景

popToRootScene

从栈中弹出全部场景直到根场景

popToSceneStackLevel 

从栈中弹出全部场景直到某个等级

(等级为0为导演,等级为1为根场景)

replaceScene

替换当前场景

end

结束游戏

pause 

暂停游戏

resume 

恢复游戏

stopAnimation

中止动画

startAnimation

开始动画

drawScene 

绘制场景

purgeCachedData 

移除全部缓存数据

setDefaultValues

基于配置信息设置默认值

setGLDefaultValues

设置OpenGL默认值

setAlphaBlending

设置OpenGL是否使用alpha通道

setDepthTest 

设置是否测试OpenGL深度

setContentScaleFactor

设置表面像素大小(不一样于屏幕大小)

getContentScaleFactor

浮点型

获取表面像素大小

getScheduler

调度类

获取时间调度对象

setScheduler

设置时间调度对象

getActionManager

动做管理类

获取动做管理对象

setActionManager

设置动做管理对象

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不愧是整个游戏的“导演”。它在游戏中无所不在,大到整个游戏的控制,小到获取屏幕的尺寸,起着相当重要的做用。

相关文章
相关标签/搜索