目錄結(jié)構(gòu):
Box2D | 物理引擎Box2D的相關(guān)文件 |
Chipmunk | 物理引擎Chipmunk的相關(guān)文件 |
cocos2dx | cocos2d-x引擎的核心,存放引擎的大部分源文件 |
CocosDenshion | 音頻模塊相關(guān)源文件 |
Debug.win32 | 在Windows上的調(diào)試輸出目錄 |
Doxygen | 生成doxygen項(xiàng)目文檔時(shí)需要的配置文件 |
HelloWorld | HelloWorld的源代碼 |
Hellolua | lua的示例代碼 |
Lua | lua腳本支持的源碼 |
Js | Js腳本支持的源碼 |
Licences | 許可文件的目錄 |
Template | 包括編譯Ios和Android平臺(tái)開發(fā)時(shí)的配置文件 |
testjs | cocos2d-x引擎js語(yǔ)言的API示例代碼 |
tests | cocos2d-x引擎的所有API示例代碼 |
Tools | 包括“Tolua的配置文件”和“Xcode4的模版生成工具” |
1.類AppDelegate //控制游戲生命周期
類AppDelegate繼承了CCApplication,定義了三個(gè)方法:
virtualbool applicationDidFinishLaunching(); //響應(yīng)窗口啟動(dòng)完成后的工作
virtualvoid applicationDidEnterBackground(); //響應(yīng)窗口進(jìn)入后臺(tái)的工作
virtualvoid applicationWillEnterForeground(); //響應(yīng)窗口從后臺(tái)恢復(fù)的工作
I 啟動(dòng)時(shí)初始化的工作
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize Director
CCDirector* pDirector =CCDirector::sharedDirector(); // 場(chǎng)景管理器
CCEGLView* pEGLView =CCEGLView::sharedOpenGLView(); //創(chuàng)建視口
pDirector->setOpenGLView(pEGLView); //設(shè)置OpenGL視口
// Set the design resolution
pEGLView->setDesignResolutionSize(designResolutionSize.width,designResolutionSize.height, kResolutionNoBorder); //設(shè)置分辨率大小
CCSize frameSize =pEGLView->getFrameSize();
vector<string> searchPath; //資源路徑
//在不同的平臺(tái)下加載不同的資源
if (frameSize.height> mediumResource.size.height)
{
searchPath.push_back(largeResource.directory);
pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height,largeResource.size.width/designResolutionSize.width));
}
// if the frame'sheight is larger than the height of small resource size, select mediumresource.
else if (frameSize.height > smallResource.size.height)
{
searchPath.push_back(mediumResource.directory);
pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height,mediumResource.size.width/designResolutionSize.width));
}
// if the frame's height is smaller than the height of medium resource size, select smallresource.
else
{
searchPath.push_back(smallResource.directory);
pDirector->setContentScaleFactor(MIN( smallResource.size.height/designResolutionSize.height , smallResource.size.width/designResolutionSize.width ) );
}
//設(shè)置資源文件路徑
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
// turn ondisplay FPS
pDirector->setDisplayStats(true); //開啟顯示FPS
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 /60); // 設(shè)置幀速率 ,最好不要低于30幀
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene(); //調(diào)用靜態(tài)方法創(chuàng)建一個(gè)場(chǎng)景,HelloWorld中會(huì)負(fù)責(zé)場(chǎng)景的實(shí)現(xiàn)
pDirector->runWithScene(pScene); //導(dǎo)演調(diào)用,運(yùn)行HelloWorld中的場(chǎng)景。
return true;
}
II 暫停動(dòng)作
// 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()->stopAnimation(); //暫停活動(dòng)
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); //暫停背景音樂(lè)
}
III 恢復(fù)動(dòng)作
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
CCDirector::sharedDirector()->startAnimation(); // 恢復(fù)活動(dòng)
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); // 恢復(fù)背景音樂(lè)
}
2.HelloWorld
CCScene*HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); //創(chuàng)建場(chǎng)景
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); //創(chuàng)建圖層
// add layer as achild to scene
scene->addChild(layer); //添加圖層作為節(jié)點(diǎn)
// return the scene
return scene;
}
bool HelloWorld::init()
{
// 1. super initfirst
if (!CCLayer::init() ) //創(chuàng)建圖層
{
return false;
}
CCSize visibleSize =CCDirector::sharedDirector()->getVisibleSize(); //獲取大小
CCPoint origin =CCDirector::sharedDirector()->getVisibleOrigin(); //獲取原點(diǎn),原點(diǎn)(origin.x , origin.y)在左下角
CCMenuItemImage *pCloseItem =CCMenuItemImage::create( "CloseNormal.png, "CloseSelected.png", this,
menu_selector( HelloWorld::menuCloseCallback ) ); //創(chuàng)建關(guān)閉按鈕 ,在這里的圖片路徑如果不是在/Resources 目錄下,則圖片不能使用。 如果要在Resources目下放置文件夾,則需要在字符串中加入路徑名稱,如”/raster-32x32/ 32x32.png“
//設(shè)置按鈕位置,可以看出 Cocos2d-x的坐標(biāo)原點(diǎn)是左下角
pCloseItem->setPosition(ccp(origin.x+ visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y +pCloseItem->getContentSize().height/2));
// visibleSize.width 屏幕的寬度
// pCloseItem->getContentSize().width/2 是圖片寬度的1/2
// pCloseItem->getContentSize().height/2 是圖片高度的1/2
// pCloseItem->setPosition(ccp(origin.x , origin.y ) ); // pCloseItem的坐標(biāo)設(shè)置是以默認(rèn)錨點(diǎn)為坐標(biāo)中心,即錨點(diǎn)在(0,0),此時(shí)按鈕只能顯示1/4
//創(chuàng)建菜單,將關(guān)閉按鈕加入到菜單項(xiàng)
CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
pMenu->setPosition(CCPointZero); // 設(shè)置菜單的位置
this->addChild(pMenu,1); // 將菜單加入到HelloWorld圖層中
//創(chuàng)建 HelloWorld 文本
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World","Arial",TITLE_FONT_SIZE);
// position thelabel on the center of the screen
pLabel->setPosition(ccp(origin.x +visibleSize.width/2,
origin.y +visibleSize.height - pLabel->getContentSize().height)); //設(shè)置文本(Label)的位置 ,坐標(biāo)是字符串中心的坐標(biāo),并不是最開始的位置
// add the labelas a child to this layer
this->addChild(pLabel,1); // 將文本(Label)加入到HelloWorld圖層中
//創(chuàng)建精靈圖片
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position thesprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y)); // 設(shè)置圖片精靈的位置
// add the spriteas a child to this layer
this->addChild(pSprite,0); // 將圖片精靈加入到HelloWorld圖層中 (第二個(gè)參數(shù)是Z軸坐標(biāo):0在底層,1在上層 。 Z軸坐標(biāo)系參考笛卡爾右手坐標(biāo)系(正方向:x軸向右,y軸向上,z軸向外)
//設(shè)置為相同的Z軸坐標(biāo)時(shí),按加載順序顯示,先加載的先顯示,后加載的后顯示
return true;
}
IIII 點(diǎn)擊按鈕結(jié)束的回調(diào)函數(shù)
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#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::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前名稱:三:cocos2d-x代碼分析-創(chuàng)新互聯(lián)
瀏覽路徑:http://vcdvsql.cn/article38/cscisp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、虛擬主機(jī)、企業(yè)建站、品牌網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容