Cocos2d-x進修入門之HelloWorld法式。本站提示廣大學習愛好者:(Cocos2d-x進修入門之HelloWorld法式)文章只能為提供參考,不一定能成為您想要的結果。以下是Cocos2d-x進修入門之HelloWorld法式正文
1、媒介:
Cocos2d-x是今朝異常風行的開源挪動2D游戲框架。本文HelloWorld示例法式中應用的Cocos2d-x版本是2.0,重要完成一個簡略的入門法式。
2、HelloWorld法式:
HelloWorld法式是許多編程說話的入門法式,關於法式員來講異常主要。
翻開本文項目後可以看到AppDelegate.h/.cpp和HelloWorldScene.h/.cpp四個文件,比普通初學編程看到的HelloWorld要稍顯龐雜。
詳細代碼以下:
#include "AppDelegate.h"
#include "HelloWorldScene.h"
USING_NS_CC;
AppDelegate::AppDelegate() {
}
AppDelegate::~AppDelegate()
{
}
bool AppDelegate::applicationDidFinishLaunching() {
// 初始化CCDirector對象
CCDirector* pDirector = CCDirector::sharedDirector();
// 初始化CCEGLView對象,CCEGLView是顯示窗口,擔任窗口級其余功效治理和完成,包含坐標和縮放治理、繪圖對象、按鍵事宜
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
// 將pEGLView傳遞給pDirector
pDirector->setOpenGLView(pEGLView);
// 翻開狀況顯示,包含FPS等
pDirector->setDisplayStats(true);
// 設置FPS,每秒刷新若干幀畫面,默許是1秒60幀,幀數越高畫面越流利,但也越耗電
pDirector->setAnimationInterval(1.0 / 60);
// 創立一個HelloWorld場景,可以或許主動釋放
CCScene *pScene = HelloWorld::scene();
// 運轉HelloWorld場景
pDirector->runWithScene(pScene);
return true;
}
// 來電或許運用進動手機後台將挪用此辦法
void AppDelegate::applicationDidEnterBackground() {
// 停滯一切動畫
CCDirector::sharedDirector()->stopAnimation();
// 假如應用了SimpleAudioEngine(掌握配景音樂等),在此處挪用暫停
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
// 當運用從後台恢復至前台將挪用此辦法
void AppDelegate::applicationWillEnterForeground() {
// 恢復一切動畫
CCDirector::sharedDirector()->startAnimation();
// 在此處挪用SimpleAudioEngine的恢復
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
AppDelegate對Cocos2d-x引擎停止了初始化,並對停止一些全局性的設置。
然則在這個外面並沒有看到詳細的界面完成,由於界面完成都在HelloWorldScene中。
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
// 創立一個Scene
CCScene *scene = CCScene::create();
// 創立一個HelloWorld的圖層(HelloWorld繼續自CCLayer)
HelloWorld *layer = HelloWorld::create();
// 將創立的HelloWorld圖層添加至之前創立的場景中
scene->addChild(layer);
// 前往創立的場景
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
/////////////////////////////
// 1. 挪用父類的初始化,假如初始化掉敗,則不會持續往下履行
if ( !CCLayer::init() )
{
// 前往false表現初始化掉敗
return false;
}
//獲得可顯示區域年夜小
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
//獲得可顯示區域坐標終點
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
/////////////////////////////
// 2. 添加一個可點擊的菜單按鈕,點擊後封閉法式
// 創立一個圖片菜單選項
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(// 挪用創立辦法
"CloseNormal.png",// 設置未點擊時菜單圖片
"CloseSelected.png",// 設置點擊時刻菜單圖片
this,// ?這個參數是甚麼
menu_selector(HelloWorld::menuCloseCallback));// 設置菜單點擊時光的回調監聽
// 設置菜單的地位坐標,pCloseItem->getContentSize()用來獲得菜單選項年夜小
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
// 創立菜單(菜單選項須要添加到菜單裡能力應用),create函數中可以添加多個菜單選項,以NULL停止添加
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
// 設置菜單的坐標(CCPointZero是坐標(0,0))
pMenu->setPosition(CCPointZero);
// 將菜單添加至HelloWorld圖層中,1是菜單在HelloWorld圖層中Z軸地位,數值越年夜,顯示的層級越高,不容易被遮擋
this->addChild(pMenu, 1);
/////////////////////////////
// 3. 添加文字控件和配景圖片
// 創立一個文件控件,create函數中參數分離是“控件須要顯示的文字”,“控件文字字體”,“控件文字字號”
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
// 設置文件控件地位(此公式盤算的地位為屏幕中心)
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// 將文字控件添加至HelloWorld圖層中
this->addChild(pLabel, 1);
// 創立一個精靈(後續將引見精靈的詳細用途,這裡精靈是配景圖片的載體)
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// 設置配景圖片地位(此公式盤算的地位為屏幕中心)
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// 將配景圖片添加至HelloWorld圖層中,並設置Z軸為0,置於菜單和文字之下
this->addChild(pSprite, 0);
// 前往true表現初始化勝利
return true;
}
// 封閉按鈕的回調函數,pSender傳遞的是挪用了該函數的對象
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// 宏界說,斷定能否是WinRT或許WP8裝備
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
// 彈出對話框,提醒文字信息
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
// 挪用CCDirector的end()函數,停止游戲
CCDirector::sharedDirector()->end();
// 宏界說,斷定能否是IOS裝備
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
// 直接挪用exit(0)停止游戲
exit(0);
#endif
#endif
}
HelloWorldScene文件是全部HelloWorld工程的焦點,從代碼中我們不難發明,在Cocos2d-x的坐標系盤算中,默許將屏幕左下角設置為坐標原點,向上方和右方遞增Y軸X軸。而設置控件地位的時刻,是以控件的中間為錨點,固然,錨點是可以經由過程代碼轉變的,這裡我們須要挪用setAnchorPoint()函數。
願望本文所述實例關於年夜家進修Cocos2d-x能起到必定的贊助感化。