這里我們來看一下cocos自動給我們生成的工程里有些什么東西,並且分析一下這些代碼的用途,來為我們以后編寫cocos程序鋪下基礎。
這里我建議看我這份隨筆的看官先看看cocos官網的快速入門手冊,不然可能會比較迷糊(因為待會要分析一些代碼,如果以前沒見過的話會比較昏)。傳送門在這里
其中一些基本不需要程序員干涉的代碼我可能會不予分析。你也可以查看官方API手冊。傳送門在這里
下面的代碼分析,如果是非常有用的東西我會在分析中用藍色標出。
首先我們進入相關的系統(你的如果是mac就打開proj.ios_mac文件夾下的工程,是windows就打開proj.win32文件夾下的工程,以此類推)的工程。我這里是mac,運行一下cocos給我們生成的代碼,結果如下:
既然是自己的demo嘛,當然要給自己打個廣告啦(那個居中的圖標顯然是cocos的logo)
這個界面里面有一些我們可以直接看出來的東西:
這里所有的元素都是我根據以往用過的引擎猜測的,實際上我們還是要看一下代碼。不過我們目前知道大概有這么些東西了,待會可以針對着看一下。
然后我們可以看看這些資源在哪里,我通過XCODE可以直接看到:
這里logo是HelloWorld.png,右下角的開關機圖標是CloseNormal.png,而那個CloseSelected.png是按下按鈕的圖片。
分析AppDelegate
好的,到我們的分析階段了。AppDelegate分為頭文件和實現文件。我們當然是先看頭文件啦。這個文件在Classes文件夾下。
1 #ifndef _APP_DELEGATE_H_ 2 #define _APP_DELEGATE_H_ 3 4 #include "cocos2d.h" 5 6 /** 7 @brief The cocos2d Application. 8 9 Private inheritance here hides part of interface from Director. 10 */ 11 class AppDelegate : private cocos2d::Application 12 { 13 public: 14 AppDelegate(); //構造函數 15 virtual ~AppDelegate(); //析構函數 16 17 virtual void initGLContextAttrs(); //這個暫時不知道是干什么的 18 19 /** 20 @brief Implement Director and Scene init code here. 21 @return true Initialize success, app continue. 22 @return false Initialize failed, app terminate. 23 */ 24 virtual bool applicationDidFinishLaunching(); 25 26 /** 27 @brief Called when the application moves to the background 28 @param the pointer of the application 29 */ 30 virtual void applicationDidEnterBackground(); 31 32 /** 33 @brief Called when the application reenters the foreground 34 @param the pointer of the application 35 */ 36 virtual void applicationWillEnterForeground(); 37 }; 38 39 #endif // _APP_DELEGATE_H_
第4行包含了cocos2d的頭文件。
第11行定義了AppDelegate類,繼承自cocos2d的Application類。
這里堆AppDelegate類的幾個虛函數在注釋上都有一定的說明了:
第24行的applicationDidFinishLaunching()是在程序初始化的時候自動調用的函數。在這里面我們可以初始化導演(Director)和場景(Scence)。如果程序初始化成果會返回True,否則返回False。
第30行的applicationDidEnterBackground()是在程序失去焦點的時候調用。這里面一般是加入用來停止程序的代碼。
第36行的applicationWillEnterForeground()是在程序獲得焦點的時候調用,可以在里面加入繼續游戲的代碼。
接下來看看實現文件:
1 #include "AppDelegate.h" 2 #include "HelloWorldScene.h" 3 4 // #define USE_AUDIO_ENGINE 1 5 // #define USE_SIMPLE_AUDIO_ENGINE 1 6 7 #if USE_AUDIO_ENGINE && USE_SIMPLE_AUDIO_ENGINE 8 #error "Don't use AudioEngine and SimpleAudioEngine at the same time. Please just select one in your game!" 9 #endif 10 11 #if USE_AUDIO_ENGINE 12 #include "audio/include/AudioEngine.h" 13 using namespace cocos2d::experimental; 14 #elif USE_SIMPLE_AUDIO_ENGINE 15 #include "audio/include/SimpleAudioEngine.h" 16 using namespace CocosDenshion; 17 #endif 18 19 USING_NS_CC; 20 21 static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); 22 static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); 23 static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); 24 static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); 25 26 AppDelegate::AppDelegate() 27 { 28 } 29 30 AppDelegate::~AppDelegate() 31 { 32 #if USE_AUDIO_ENGINE 33 AudioEngine::end(); 34 #elif USE_SIMPLE_AUDIO_ENGINE 35 SimpleAudioEngine::end(); 36 #endif 37 } 38 39 // if you want a different context, modify the value of glContextAttrs 40 // it will affect all platforms 41 void AppDelegate::initGLContextAttrs() //這個真心沒看懂是個啥 42 { 43 // set OpenGL context attributes: red,green,blue,alpha,depth,stencil,multisamplesCount 44 GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8, 0}; 45 46 GLView::setGLContextAttrs(glContextAttrs); 47 } 48 49 // if you want to use the package manager to install more packages, 50 // don't modify or remove this function 51 static int register_all_packages() 52 { 53 return 0; //flag for packages manager 54 } 55 56 bool AppDelegate::applicationDidFinishLaunching() { 57 // initialize director 58 auto director = Director::getInstance(); 59 auto glview = director->getOpenGLView(); 60 if(!glview) { 61 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) 62 glview = GLViewImpl::createWithRect("TestGame", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); 63 #else 64 glview = GLViewImpl::create("TestGame"); 65 #endif 66 director->setOpenGLView(glview); 67 } 68 69 // turn on display FPS 70 director->setDisplayStats(true); 71 72 // set FPS. the default value is 1.0/60 if you don't call this 73 director->setAnimationInterval(1.0f / 60); 74 75 // Set the design resolution 76 glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); //不知道是干啥的 77 auto frameSize = glview->getFrameSize(); 78 // if the frame's height is larger than the height of medium size. 79 if (frameSize.height > mediumResolutionSize.height) 80 { 81 director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); 82 } 83 // if the frame's height is larger than the height of small size. 84 else if (frameSize.height > smallResolutionSize.height) 85 { 86 director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); 87 } 88 // if the frame's height is smaller than the height of medium size. 89 else 90 { 91 director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); 92 } 93 94 register_all_packages(); 95 96 // create a scene. it's an autorelease object 97 auto scene = HelloWorld::createScene(); 98 99 // run 100 director->runWithScene(scene); 101 102 return true; 103 } 104 105 // This function will be called when the app is inactive. Note, when receiving a phone call it is invoked. 106 void AppDelegate::applicationDidEnterBackground() { 107 Director::getInstance()->stopAnimation(); 108 109 #if USE_AUDIO_ENGINE 110 AudioEngine::pauseAll(); 111 #elif USE_SIMPLE_AUDIO_ENGINE 112 SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 113 SimpleAudioEngine::getInstance()->pauseAllEffects(); 114 #endif 115 } 116 117 // this function will be called when the app is active again 118 void AppDelegate::applicationWillEnterForeground() { 119 Director::getInstance()->startAnimation(); 120 121 #if USE_AUDIO_ENGINE 122 AudioEngine::resumeAll(); 123 #elif USE_SIMPLE_AUDIO_ENGINE 124 SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 125 SimpleAudioEngine::getInstance()->resumeAllEffects(); 126 #endif 127 }
第18行以上的亂七八糟的東西我們就先不看了,無非就是包含頭文件啊,使用命名空間什么的。這些暫時不管。
第19行是一個宏,可以進入到里面看看(這個宏后面用到的還比較多):
#define USING_NS_CC using namespace cocos2d
可以看到就是使用cocos2d的命名空間。
- 第21~24行定義了四種大小(從cocos2d::Size可以看出是大小的定義),按照變量名字分別是設計時大小,最小化大小,通常化大小和最大化大小。這里我還是要重復說一下:現在這些只是我們的猜測,具體的還是要看到相關代碼才行。
那么我們可以想,我是不是更改一下這些玩意就可以改變窗口大小了呢?嗯……可以嘗試一下,就先從designResolutionSize下手吧。
我這里將designResolutionSize參數改成1024,480,果然窗口的大小改變了:
不過改變了其他三個尺寸之后窗口沒什么變化。暫時先不管吧,反正現在知道designResolutionSize變量存儲的是當前窗口的大小就行了
- 30~37行是析構函數的實現。可以看出是釋放了AudioEngine和SimpleAudioEngine兩個模塊。
- 51行的函數用於管理包。當你想要使用包管理器(Package Manager)來安裝多個包的時候,就不要修改或者刪除這里的代碼。(你問我包是啥?對不起我也不曉得。你說那你怎么知道這個是包管理器函數?看注釋啊🙂)
- 56~103是個大函數,為程序初始化的時候調用的函數。讓我們來慢慢分析:
- 58行初始化了導演。按照官方快速入門手冊的說法,導演(Direct)是一個單例對象(關於單例請見單例模式),主要用於切換場景啊,維護層啊什么的,就和我們生活中的導演差不多。
- 59~67行初始化了視圖。並且對視圖創建失敗做了一定的檢測。最后將這個視圖指定為Direct的視圖。
- 70行打開了FPS顯示。這里你可以把參數改為false,運行程序你會發下左下角的調試信息消失了。這個比較重要,因為我們的游戲最后發布的時候是不需要顯示FPS信息的。
- 73行設置了這個程序的幀率,即FPS。從參數可以看出是60幀每秒(FPS=60)。這個到時候在制作自己的游戲的時候可以根據實際情況改一改。
- 79~92行分別對窗口大小大於smallResolutionSize ,mediumResolutionSize ,largeResolutionSize時做出行動。這里的setContentScaleFactor函數是“改變Surface里像素的大小”。
- 96行創建了一個場景(Scene)。根據官方手冊知道場景是用來容納物體(比如Sprite啊什么的)的。
- 100行說明在程序開始的時候運行這個場景
- 接下來的106~115行是用來指定窗口獲得焦點時的相關行動
- 118~127則是窗口失去焦點的相關行動
這里關於APPDelegate文件就分析完了。我們看出來好像這個文件里面沒有什么和我們一開始看到的界面元素有關系。好像都是一些對游戲初始化的工作。所以以后我們不會大幅度更改這里面的代碼。
分析HelloWorldScene文件
那么首先還是來看看其頭文件:
1 #ifndef __HELLOWORLD_SCENE_H__ 2 #define __HELLOWORLD_SCENE_H__ 3 4 #include "cocos2d.h" 5 6 class HelloWorld : public cocos2d::Scene 7 { 8 public: 9 static cocos2d::Scene* createScene(); 10 11 virtual bool init(); 12 13 // a selector callback 14 void menuCloseCallback(cocos2d::Ref* pSender); 15 16 // implement the "static create()" method manually 17 CREATE_FUNC(HelloWorld); 18 }; 19 20 #endif // __HELLOWORLD_SCENE_H__
這個頭文件挺簡單的,定義了一個HelloWorld類繼承自cocos2d::Scene。
- 第14行的函數定義了一個菜單關閉的回調函數(你說,我咋沒在界面上看到有菜單啊?別急,讓我們來一步一步看看這個菜單是從哪里來的)
- 第17行使用了CREATE_FUNC宏,用於創建層。我們可以跳轉到其定義看一下:
-
#define CREATE_FUNC(__TYPE__) \ static __TYPE__* create() \ { \ __TYPE__ *pRet = new(std::nothrow) __TYPE__(); \ if (pRet && pRet->init()) \ { \ pRet->autorelease(); \ return pRet; \ } \ else \ { \ delete pRet; \ pRet = nullptr; \ return nullptr; \ } \ }
顯然,這個宏其實是定義了一個static __TYPE__* create()函數。
接下來看看實現文件:
1 #include "HelloWorldScene.h" 2 #include "SimpleAudioEngine.h" 3 4 USING_NS_CC; 5 6 Scene* HelloWorld::createScene() 7 { 8 return HelloWorld::create(); 9 } 10 11 // Print useful error message instead of segfaulting when files are not there. 12 static void problemLoading(const char* filename) 13 { 14 printf("Error while loading: %s\n", filename); 15 printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n"); 16 } 17 18 // on "init" you need to initialize your instance 19 bool HelloWorld::init() 20 { 21 ////////////////////////////// 22 // 1. super init first 23 if ( !Scene::init() ) 24 { 25 return false; 26 } 27 28 auto visibleSize = Director::getInstance()->getVisibleSize(); 29 Vec2 origin = Director::getInstance()->getVisibleOrigin(); 30 31 ///////////////////////////// 32 // 2. add a menu item with "X" image, which is clicked to quit the program 33 // you may modify it. 34 35 // add a "close" icon to exit the progress. it's an autorelease object 36 auto closeItem = MenuItemImage::create( 37 "CloseNormal.png", 38 "CloseSelected.png", 39 CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 40 41 if (closeItem == nullptr || 42 closeItem->getContentSize().width <= 0 || 43 closeItem->getContentSize().height <= 0) 44 { 45 problemLoading("'CloseNormal.png' and 'CloseSelected.png'"); 46 } 47 else 48 { 49 float x = origin.x + visibleSize.width - closeItem->getContentSize().width/2; 50 float y = origin.y + closeItem->getContentSize().height/2; 51 closeItem->setPosition(Vec2(x,y)); 52 } 53 54 // create menu, it's an autorelease object 55 auto menu = Menu::create(closeItem, NULL); 56 menu->setPosition(Vec2::ZERO); 57 this->addChild(menu, 1); 58 59 ///////////////////////////// 60 // 3. add your codes below... 61 62 // add a label shows "Hello World" 63 // create and initialize a label 64 65 auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); 66 if (label == nullptr) 67 { 68 problemLoading("'fonts/Marker Felt.ttf'"); 69 } 70 else 71 { 72 // position the label on the center of the screen 73 label->setPosition(Vec2(origin.x + visibleSize.width/2, 74 origin.y + visibleSize.height - label->getContentSize().height)); 75 76 // add the label as a child to this layer 77 this->addChild(label, 1); 78 } 79 80 // add "HelloWorld" splash screen" 81 auto sprite = Sprite::create("HelloWorld.png"); 82 if (sprite == nullptr) 83 { 84 problemLoading("'land.png'"); 85 } 86 else 87 { 88 // position the sprite on the center of the screen 89 sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 90 91 // add the sprite as a child to this layer 92 this->addChild(sprite, 0); 93 } 94 return true; 95 } 96 97 98 void HelloWorld::menuCloseCallback(Ref* pSender) 99 { 100 //Close the cocos2d-x game scene and quit the application 101 Director::getInstance()->end(); 102 103 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 104 exit(0); 105 #endif 106 107 }
- 第六行的createScene()函數調用了HelloWorld類的create方法創建了一個層。你說誒不對啊,上面那個HelloWorld類的定義里面沒有create方法呀。這個時候你可以回過頭再去看看CREATE_FUNC宏的代碼。其實CREATE_FUNC函數就是create函數啦。這個函數被APPDelegate類調用過(見APPDelegate的實現文件97行)
- 12~16行定義的函數用於輸出錯誤信息 19~95行定義了HelloWorld類的init函數。這個函數是在CREATE_FUNC里面被調用的。我們來看看這個函數里面到底有些啥
-
- 23~26行為第一步——創建Scene
- 36~57創建菜單(原來菜單在這里,趕緊看看那這個菜單是什么)
- 首先36行通過MenuItemImage的create方法創建一個菜單組件。注意到這里面的參數!這個參數正是我們前面在界面上看到的開關機圖像!原來那個東西不是按鈕,是一個菜單。
- 41~52行判斷是否創建菜單項成功,並且調整菜單項位置為右下角
- 55行通過菜單組件創建一個菜單
- 56行設置菜單位置
- 57行將菜單放到層的樹里面(就是放到層中)
- 65~95行,注釋上寫着“加入你自己的代碼”。我們來看看cocos給我們加了什么代碼
- 65行創建了一個label,而且是使用TTF字體創建的。用於顯示HelloWorld(果然HelloWorld是個label)
- 66~78行判斷label是否創建成功,並且設置了label的位置,加入了層。
- 81行創建了一個Sprite,也就是精靈。用於繪制那個logo。
- 82~93行同樣是判斷精靈是否創建成功,然后修改位置,加入層。
- 最后98~107定義了菜單的回調函數。這里如果點擊了就會推出程序。然后這里有一個比較重要的東西:CC_TARGET_PLATFORM常量。這個常量存儲着這個程序運行的操作系統。這里判斷是否為IOS操作系統。我們可以使用這個常量來獲得當前操作系統。
那么知道了這些之后,你可以更改一下代碼來產生自己的界面。我這里改變了label的顯示,並且改變了logo(把你要替換logo的圖片放在Resource下,而且最好是png。我一開始使用bmp不能讀取):
至此,我們的demo就分析完成了。
函數調用的過程
可能看完上面的分析有些人會有些頭昏:“這說的都是啥和啥啊我現在腦子里一片混亂”。沒關系,下面我們使用函數之間的調用圖來說明一下這四個文件到底干了什么:
這下清楚許多了吧。
總結
現在我們來總結一些常用的代碼:
- designResolutionSize變量用於設定窗口大小
- director->setDisplayStats(true);用於設置是否顯示FPS
- director->setAnimationInterval(1.0f / 60);用於設置FPS
其他的代碼,有的涉及到菜單和label的,我們放到以后的專題去說。