Cocos2d-x 2.0 從HelloWorld入手

       從上一篇《Cocos2d-x 2.0 在Windows平臺下的使用》已經初步瞭解了Cocos2d-x的安裝、編譯,也已經可以運行HelloWorld示例了,運行HelloWorld至少所需的文件,包括:素材、動態庫,我們把"...\Release.win32"裏的"HelloWorld.exe"單獨拷貝到一個新文件夾,至少所需的文件如下圖所示:

打開cocos2d-win32.vc2008.sln,展開"HelloWorld"工程,工程結構如下所示:

對應"...\HelloWorld"文件夾的結構如下所示:

可知"Classes"放置遊戲開發邏輯相關、平臺無關的代碼,"Resources"放置圖片、聲音、配置等資源,"win32"等放置平臺相關的代碼。
 
瞭解如何運行,在win32下就從main()函數開始,打開"main.cpp",代碼如下所示: 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "main.h"
#include "../Classes/AppDelegate.h"
#include "CCEGLView.h"

USING_NS_CC;

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // create the application instance
    AppDelegate app;
    CCEGLView& eglView = CCEGLView::sharedOpenGLView();
    eglView.setViewName("Hello World");
    eglView.setFrameSize(480320);
    // set the design resolution screen size, if you want to use Design Resoulution scaled to current screen, please uncomment next line.
    // eglView.setDesignResolutionSize(480, 320);
    return CCApplication::sharedApplication().run();
}
其中AppDelegate是最主要的類,代理運行着整個應用程序,它的體系如下:

簡化流程如下所示:

CCEGLView是整個程序窗口類,遊戲的顯示、表現等等都在這上面進行,跟AppDelegate合起來的流程如下:

CCApplication::run()中調用了applicationDidFinishLaunching()函數,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool AppDelegate::applicationDidFinishLaunching() { 
    // initialize director
    CCDirector *pDirector = CCDirector::sharedDirector();
 
    pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());
 
    // enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
    // pDirector->enableRetinaDisplay(true);
 
    // turn on display FPS
    pDirector->setDisplayStats(true);
 
    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);
 
    // create a scene. it's an autorelease object
    CCScene *pScene = HelloWorld::scene();
 
    // run
    pDirector->runWithScene(pScene);
 
    return true;
所做的事情包括:初始化導演類CCDirector 、創建HelloWorld場景、運行場景。HelloWorld場景從CCLayer派生,創建HelloWorld場景所調用如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
CCScene* HelloWorld::scene() 
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
 
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();
 
    // add layer as a child to scene
    scene->addChild(layer);
 
    // return the scene
    return scene;
實質是創建一個場景類,再創建HelloWorld層,把層放到場景上。創建HelloWorl層的時候,調用了init()函數,init()函數如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
bool HelloWorld::init() 
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
 
    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    // you may modify it.
 
    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback) );
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 2020) );
 
    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);
 
    /////////////////////////////
    // 3. add your codes below...
 
    // add a label shows "Hello World"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World""Arial"24);
    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();
 
    // position the label on the center of the screen
    pLabel->setPosition( ccp(size.width / 2, size.height - 50) );
 
    // add the label as a child to this layer
    this->addChild(pLabel, 1);
 
    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");
 
    // position the sprite on the center of the screen
    pSprite->setPosition( ccp(size.width/2, size.height/2) );
 
    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
 
    return true;
在這個函數裏,創建了各個子元素,創建完之後,用addChild加入到主層上,上面的註釋清晰的描述了步驟。
 
參考閱讀:
1.cocos2d-x初探學習筆記(1)--HelloWorld http://blog.csdn.net/bill_man/article/details/7202458
2.cocos2d-x之HelloWorld範例分析(一) http://www.lugw.net/?p=80005

發佈了21 篇原創文章 · 獲贊 11 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章