Cocos2d-x 剖析HelloWorld

來源:互聯網
上載者:User

搭建好環境之後的第一件事,就是如何在搭建好的環境下運行我們的第一個程式HelloWorld。下面來剖析一下HelloWorld。

本人菜鳥,只想把學到的跟大家分享,有錯請指出交流,不喜勿噴。

剖析之前需先掌握引擎的基本知識,這裡有個連結:http://www.cnblogs.com/lhming/category/391396.html

建立工程HelloWorld:

這時可以看到會在工程裡面新增這些檔案:

include:存放標頭檔

resource:存放資源檔

source:存放CPP源檔案

在這些檔案中,main.cpp是win32程式入口,AppDelegate是應用真正的入口,HelloWorldScene是情境類。接下來一個個分析:

1、AppDelegate:

AppDelegate.h:

#ifndef __APP_DELEGATE_H__#define __APP_DELEGATE_H__#include "cocos2d.h"/**@brief    The cocos2d Application.The reason for implement as private inheritance is to hide some interface call by CCDirector.*/class  AppDelegate : private cocos2d::CCApplication{public:    AppDelegate();    virtual ~AppDelegate();    /**    @brief    Implement CCDirector and CCScene init code here.    @return true    Initialize success, app continue.    @return false   Initialize failed, app terminate.    */    virtual bool applicationDidFinishLaunching();    /**    @brief  The function be called when the application enter background    @param  the pointer of the application    */    virtual void applicationDidEnterBackground();    /**    @brief  The function be called when the application enter foreground    @param  the pointer of the application    */    virtual void applicationWillEnterForeground();};#endif  // __APP_DELEGATE_H__

AppDelegate();:建構函式

~AppDelegate():解構函式

bool applicationDidFinishLaunching():初始化導演類和情境類

void applicationDidEnterBackground():應用暫停時候調用

void applicationWillEnterForeground():應用重新被操作時調用

主要掌握applicationDidFinishLaunching:

2、HelloWorldScene:

HelloWorldScene.h:

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "Box2D/Box2D.h"#include "SimpleAudioEngine.h"class HelloWorld : public cocos2d::CCLayer{public:    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();      // there's no 'id' in cpp, so we recommand to return the exactly class pointer    static cocos2d::CCScene* scene();        // a selector callback    void menuCloseCallback(CCObject* pSender);    // implement the "static node()" method manually    CREATE_FUNC(HelloWorld);};#endif  // __HELLOWORLD_SCENE_H__

bool init():初始化函數

cocos2d::CCScene* scene():返回初始化好的情境

menuCloseCallback(CCObject* pSender):關閉程式

CREATE_FUNC(HelloWorld):對於CREATE_FUNC我們不知道是什麼東西,這個時候可以選中CREATE_FUNC,右鍵->轉到定義,可以看到CREATE_FUNC的定義

從上面的定義可以看出,這裡定義了HelloWorldScene *create()函數,而在這裡面則調用了HelloWorldScene::init()函數進行初始化。

在AppDelegate.cpp的applicationDidFinishLaunching()函數中可以發現在此處調用HelloWorldScene::create();

HelloWorldScene.cpp:

// 返回初始化好的情境 CCScene* HelloWorld::scene(){    CCScene * scene = NULL;    do     {        // 'scene' is an autorelease object        scene = CCScene::create();// 查看CC_BREAK_IF的定義發現:// #define CC_BREAK_IF(cond)            if(cond) break// 所以CC_BREAK_IF的作用是來判斷條件如果成立則跳出if語句        CC_BREAK_IF(! scene);        // 在這裡初始化        HelloWorld *layer = HelloWorld::create();        CC_BREAK_IF(! layer);        // 將布景添加到情境        scene->addChild(layer);    } while (0);    // 返回初始化好的情境    return scene;}

// 初始化函數bool HelloWorld::init(){    bool bRet = false;    do     {        CC_BREAK_IF(! CCLayer::init());        // 1. Add a menu item with "X" image, which is clicked to quit the program.        // Create a "close" menu item with close icon, it's an auto release object.        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(            "CloseNormal.png",// 平常狀態顯示的圖片            "CloseSelected.png",// 點擊狀態顯示的圖片            this,            menu_selector(HelloWorld::menuCloseCallback));        CC_BREAK_IF(! pCloseItem);        // Place the menu item bottom-right conner.        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));        // Create a menu with the "close" menu item, it's an auto release object.        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);        pMenu->setPosition(CCPointZero);// CCPointZero代表座標為(0,0)        CC_BREAK_IF(! pMenu);        // Add the menu to HelloWorld layer as a child layer.        this->addChild(pMenu, 1);        // 2. Add a label shows "Hello World".        // Create a label and initialize with string "Hello World".        CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);        CC_BREAK_IF(! pLabel);        // Get window size and place the label upper.         CCSize size = CCDirector::sharedDirector()->getWinSize();        pLabel->setPosition(ccp(size.width / 2, size.height - 50));        // Add the label to HelloWorld layer as a child layer.        this->addChild(pLabel, 1);        // 3. Add add a splash screen, show the cocos2d splash image.        CCSprite* pSprite = CCSprite::create("HelloWorld.png");        CC_BREAK_IF(! pSprite);        // Place the sprite on the center of the screen        pSprite->setPosition(ccp(size.width/2, size.height/2));        // Add the sprite to HelloWorld layer as a child layer.        this->addChild(pSprite, 0);        bRet = true;    } while (0);    return bRet;}

總結:

       學習時先從AppDelegate.cpp入手,遇到不懂的查看其定義,然後一步步下去你就能把代碼看懂了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.