Cocos2d-x 剖析HelloWorld

搭建好环境之后的第一件事,就是如何在搭建好的环境下运行我们的第一个程序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入手,遇到不懂的查看其定义,然后一步步下去你就能把代码看懂了。












  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值