Cocos2D-X學習筆記(2)-HelloWorld程式碼分析
阿新 • • 發佈:2020-12-19
開啟cocos2dx工程後,HelloWorld一共就四個檔案。
AppDelegate.h
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_
#include "CCApplication.h"
/**
@brief The cocos2d Application.
私有繼承的是由於CCDirector隱藏了一些介面
The reason for implement as private inheritance is to hide some interface call by CCDirector.
*/
class AppDelegate : private cocos2d::CCApplication
{
public:
AppDelegate();
virtual ~AppDelegate();
/**
初始化OpenGL例項,設定路徑等
@brief Implement for initialize OpenGL instance, set source path, etc...
*/
virtual bool initInstance();
/**
CCDirector和CCScene的初始化程式碼寫在這裡
@brief Implement CCDirector and CCScene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();
/**
當app進入後臺時呼叫此函式
@brief The function be called when the application enter background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();
/**
當app從後臺回到介面時呼叫此函式
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};
#endif // _APP_DELEGATE_H_
AppDelegate.cpp
#include "AppDelegate.h"
#include "cocos2d.h"
#include "HelloWorldScene.h"
#include "CCEGLView.h"
USING_NS_CC;
AppDelegate::AppDelegate() {
}
AppDelegate::~AppDelegate() {
}
bool AppDelegate::initInstance() {
bool bRet = false;
do {
// 下面這段#if和endif都是平臺的判斷,可以忽略
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
// Initialize OpenGLView instance, that release by CCDirector when application terminate.
// The HelloWorld is designed as HVGA.
CCEGLView * pMainWnd = new CCEGLView();
CC_BREAK_IF(! pMainWnd
|| ! pMainWnd->Create(TEXT("cocos2d: Hello World"), 480, 320));
#endif // CC_PLATFORM_WIN32
....
#if (CC_TARGET_PLATFORM == CC_PLATFORM_QNX)
CCEGLView * pMainWnd = new CCEGLView();
CC_BREAK_IF(! pMainWnd|| ! pMainWnd->Create(1024, 600));
CCFileUtils::setResourcePath("app/native/Resources");
#endif // CC_PLATFORM_QNX
bRet = true;
} while (0);
return bRet;
}
//
bool AppDelegate::applicationDidFinishLaunching() {
// 初始化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);
// 開啟左下角的FPS顯示
pDirector->setDisplayFPS(true);
// 設定FPS為每秒60幀
pDirector->setAnimationInterval(1.0 / 60);
// 建立一個場景,這個場景是一個會自動釋放的物件。 create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// 通過director執行場景
pDirector->runWithScene(pScene);
return true;
}
// 當app被後臺執行時,這個函式會呼叫。 如果有人打電話來,這個函式也會被呼叫。
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
CCDirector::sharedDirector()->pause();
// 如果使用了SimpleAudioEngine(聲音引擎),那它必須要暫停
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
// 當app從後臺回覆後,此函式會被呼叫
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
CCDirector::sharedDirector()->resume();
// 如果使用了SimpleAudioEngine(聲音引擎),那它必須要恢復播放
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
// 建立場景
CCScene *scene = CCScene::node();
// 建立layer
HelloWorld *layer = HelloWorld::node();
// 把layer加到場景裡
scene->addChild(layer);
// return the scene
return scene;
}
// 場景初始化
bool HelloWorld::init()
{
//
if ( !CCLayer::init() )
{
return false;
}
/
// 新增一個"關閉"按鈕,用於關閉程式。
// 第一個引數是 按鈕正常時圖片
// 第二個引數是 按鈕被點選時圖片
// 第三個引數指向本物件
// 第四個引數是點選後的回撥函式
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
// 設定按鈕位置
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
// 建立選單
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1);
/
// 3. add your codes below...
// 建立一個字型標籤,用於展示"Hello World"
CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Arial", 24);
// ask director the window size
CCSize size = CCDirector::sharedDirector()->getWinSize();
// 設定位置
pLabel->setPosition( ccp(size.width / 2, size.height - 50) );
// 將剛剛建立的標籤新增到layer上,層級=1
this->addChild(pLabel, 1);
// 用圖片建立一個精靈
CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");
// 設定精靈的位置
pSprite->setPosition( ccp(size.width/2, size.height/2) );
// 把精靈加到layer上,層級=0
this->addChild(pSprite, 0);
return true;
}
// 點選關閉按鈕時的回撥函式
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// 關閉dDirector
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
HelloWorldScene.h
此標頭檔案對應HelloWorldScene.cpp的註釋,就不補充了。
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.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
virtual void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
LAYER_NODE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__