搭建好環境之後的第一件事,就是如何在搭建好的環境下運行我們的第一個程序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入手,遇到不懂的查看其定義,然後一步步下去你就能把代碼看懂了。