Cocos2d-x之Director

|   版权声明:本文为博主原创文章,未经博主允许不得转载。

Director类简介

       在Cocos2d-x-3.x引擎中,采用节点树形结构来管理游戏对象,一个游戏可以划分为不同的场景,一个场景又可以分为不同的层,一个层又可以拥有任意个可见的游戏节点(即对象,游戏中基本上所有的类都派生于节点类Node)。可以执行Action来修改游戏节点的属性,使其移动、旋转、放大、缩小等等。每一个时刻都有一个场景在独立运行,通过切换不同的场景来完成一个游戏流程,游戏流程的管理由Director来执行,其基本框架类图如下:

                

  一款游戏好比一部电影,只是游戏具有更强的交互性,不过它们的基本原理是一致的。所以在Cocos2dx中把统筹游戏大局的类抽象为导演(Director),Director是整个cocos2dx引擎的核心,是整个游戏的导航仪,游戏中的一些常用操作就是由Director来控制的,比如OpenGL ES的初始化,场景的转换,游戏暂停继续的控制,世界坐标和GL坐标之间的切换,对节点(游戏元素)的控制等,还有一些游戏数据的保存调用,屏幕尺寸的获取等都要由Director类来管理控制的。因为Director是游戏项目的总导演,会经常调用进行一些控制,所以该Director利用了单件设计模式,也就是项目里取到的director都是同一个。用getInstance() 方法取得Director的实例,具体的API可以参考相关文档,就不做赘述了。

  Director类是Cocos2D-X游戏引擎的核心,它用来闯将并控制着主屏幕的显示,同时控制场景的显示时间和显示方式。Director类还可以设定游戏呈现方面,包括游戏呈现的窗口,PFS显示,默认帧率上限,纹理颜色为框等;在整个游戏里一般只有一个导演。游戏的开始、结束、暂停都会调用Director类的方法。Director类的主要功能如下:

     ◆  初始化OpenGL会话  

     ◆  设置OpenGL的一些参数和方式

     ◆  访问和改变场景以及访问Cocos2D-X的配置细节

     ◆  访问视图

     ◆  设置投影和朝向

 

Director是单例模式,调用Director方法的标准如下:

auto director = Director::getInstance();
director -> 函数名();
或者
Director::getInstance() -> 函数名();

DisplayerLinkDirector继承了Director,是一个自动刷新的导演类。它支持1/60, 1/30, 1/15三种动画帧间隔。

 

Director类的主要函数:

 画红线重要函数的讲解: 

   getVisibleOrigin:   获得当前窗口的原点坐标

   getVisibleSize:  获取可见窗口的大小,返回的是点

   getRunningScene: 获得当前正在运行的场景,导演一次只能运行一个场景。

   setOpenGLView:      设置绘制所有对象的OpenGL视图

   getOpenGLView:      获得绘制所有对象的OpenGL视图

   setProjection:      设置OpenGL投影。

   getProjection:      获取OpenGL投影。

   getWinSize:      获得当前窗口的大小,经常根据返回的窗口尺寸,定位其他节点位置(点坐标)

   convertToGL:     将屏幕坐标转换为一个OpenGL坐标。 对于将(多点)触摸坐标转换为当前布局(横向或纵向)很有用。

   convertToUI:     转换一个OpenGL坐标到屏幕坐标。 对于比如glScissor调用将node点转换到窗口点很有用。

   stopAnimation:      停止动画。不进行绘制。主循环不会再被触发。 如果你不想暂停动画,请调用            [pause]。

 

   startAnimation:  主循环触发一次。 只有之前调用过stopAnimation,才能调用这个函数。不调用这个函数来开始主循环。请调用runWithScene来开始主循环。

   end:         结束场景,同时退出应用

下面这几个函数见cocos2d-x引擎框架那里

 1 runWithScene:    启动游戏,并运行scene场景。这个方法在主程序启动时第一次启动主场景时调用
 2 
 3 popScene:           释放当前场景,再从代执行场景栈中弹出栈顶的场景,并将其设置为当前运行场景。如果栈空,则直接结束应用。
 4 
 5 pushScene:           将当前运行中的场景暂停并压入到带执行场景栈中去,再将传入的scene设置为当前运行场景。
 6 
 7 replaceScene:      直接使用传入的scene替换当前场景来切换画面,当前场景将被释放
 8 
 9 pause:                 暂停当前运行场景中的所有计时器和动作,场景任会显示在屏幕上
10 
11 resume:               恢复当前运行场景中被暂停的计时器和动作

 

导演类的使用代码:

 1 // on "init" you need to initialize your instance
 2 bool HelloWorld::init()
 3 {
 4 //////////////////////////////
 5 // 1. super init first
 6 if ( !Layer::init() )
 7 {
 8 return false;
 9 }
10 
11 Size visibleSize = Director::getInstance()->getVisibleSize();
12 Vec2 origin = Director::getInstance()->getVisibleOrigin();
13 
14 /////////余下部分省略
15 
16 return true;
17 }
18 
19 void HelloWorld::menuCloseCallback(Ref* pSender) //回调函数的功能实现
20 {
21 Director::getInstance()->end();
22 
23 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
24 exit(0);
25 #endif
26 }
27 
28 bool AppDelegate::applicationDidFinishLaunching() {
29 // initialize director
30 auto director = Director::getInstance();
31 auto glview = director->getOpenGLView();
32 if(!glview) {
33 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
34 glview = GLViewImpl::createWithRect("MyGame01", Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
35 #else
36 glview = GLViewImpl::create("MyGame01");
37 #endif
38 director->setOpenGLView(glview);
39 }
40 
41 // turn on display FPS
42 director->setDisplayStats(true);
43 
44 // set FPS. the default value is 1.0/60 if you don't call this
45 director->setAnimationInterval(1.0 / 60);
46 
47 // Set the design resolution
48 glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
49 Size frameSize = glview->getFrameSize();
50 // if the frame's height is larger than the height of medium size.
51 if (frameSize.height > mediumResolutionSize.height)
52 { 
53 director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
54 }
55 // if the frame's height is larger than the height of small size.
56 else if (frameSize.height > smallResolutionSize.height)
57 { 
58 director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
59 }
60 // if the frame's height is smaller than the height of medium size.
61 else
62 { 
63 director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
64 }
65 
66 register_all_packages();
67 
68 // create a scene. it's an autorelease object
69 auto scene = MyGame::createScene();
70 
71 // run
72 director->runWithScene(scene);
73 
74 return true;
75 }
76 
77 // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
78 void AppDelegate::applicationDidEnterBackground() {
79 Director::getInstance()->stopAnimation();
80 
81 // if you use SimpleAudioEngine, it must be pause
82 // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
83 }
84 
85 // this function will be called when the app is active again
86 void AppDelegate::applicationWillEnterForeground() {
87 Director::getInstance()->startAnimation();
88 
89 // if you use SimpleAudioEngine, it must resume here
90 // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
91 }                                                                 
原文地址:https://www.cnblogs.com/geore/p/5794035.html