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不愧是整個遊戲的「導演」。它在遊戲中無所不在,大到整個遊戲的控制,小到獲取屏幕的尺寸,起着相當重要的做用。

相關文章
相關標籤/搜索