如何制作一个简单的游戏 Cocos2d x 2 0 4
分享一下我老師大神的人工智能教程!零基礎(chǔ),通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉(zhuǎn)載本篇文章。分享知識,造福人民,實(shí)現(xiàn)我們中華民族偉大復(fù)興!
本文實(shí)踐自 Ray Wenderlich 的文章《How To Make A Simple iPhone Game with Cocos2D 2.X Tutorial》,文中使用Cocos2D,我在這里使用Cocos2D-x 2.0.4進(jìn)行學(xué)習(xí)和移植,前者是用Object-C所寫,所以移植到Cocos2D-x會有些差異,比如某些函數(shù)、某些功能不能跟原文一樣直接實(shí)現(xiàn),需另轉(zhuǎn)換方法實(shí)現(xiàn)。之前已經(jīng)對Cocos2D-x的安裝以及簡單使用進(jìn)行了介紹,這里不再介紹,直接進(jìn)入主題。
?
步驟如下:
1.新建Cocos2d-win32工程,工程名為"SimpleGame",去除"Box2D"選項(xiàng),勾選"Simple Audio Engine in Cocos Denshion"選項(xiàng);
2.編譯運(yùn)行,可以看到如下圖所示:
3.下載本游戲所需的資源,將資源放置"Resources"目錄下;
4.游戲需要一個白色的背景,最簡單的方法是使用CCLayerColor,將HelloWorldScene.h文件"HelloWorld"類改為如下:?
| 1 | class?HelloWorld?:?public?cocos2d::CCLayerColor |
首先添加玩家,讓玩家位于左邊屏幕中間,將HelloWorldScene.cpp文件的init函數(shù),改為如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
bool?HelloWorld::init() { ????bool?bRet?=?false; ????do? ????{ ????????CC_BREAK_IF(!?CCLayerColor::initWithColor(ccc4(255,?255,?255,?255))); ????????CCSize?winSize?=?CCDirector::sharedDirector()->getWinSize(); ????????bRet?=?true; ????return?bRet; |
5.編譯運(yùn)行,可以看到玩家精靈在白色背景上,如下圖所示:
6.接下來添加怪物,并且讓怪物可以移動,我們在屏幕右邊創(chuàng)建怪物,建立動作讓它們向左移動,增加addMonster方法,代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
void?HelloWorld::addMonster() { ????CCSprite?*monster?=?CCSprite::create("monster.png"); ????CCSize?winSize?=?CCDirector::sharedDirector()->getWinSize(); ????monster->setPosition(ccp(winSize.width?+?monster->getContentSize().width?/?2,?actualY)); ????int?minDuration?=?2.0; ????CCMoveTo?*actionMove?=?CCMoveTo::create(actualDuration,?ccp(-monster->getContentSize().width?/?2,?actualY)); |
在右邊屏幕以隨機(jī)的位置添加怪物精靈,注意計(jì)算精靈的位置坐標(biāo),默認(rèn)描點(diǎn)在中心,不要讓怪物截?cái)嗔恕H缓笤僖?~4秒的隨機(jī)總時間,讓怪物從右邊移動到左邊,移動出邊界后,即回調(diào)函數(shù)spriteMoveFinished,進(jìn)行刪除精靈對象,增加的spriteMoveFinished方法如下:
| 1 2 3 4 5 |
void?HelloWorld::spriteMoveFinished(CCNode?*sender) { ????CCSprite?*sprite?=?(CCSprite*)sender; ????this->removeChild(sprite,?true); } |
接下去就是定時創(chuàng)建怪物,在init函數(shù)返回之前,安裝定時器,每秒執(zhí)行一次,代碼如下:
| 1 | this->schedule(schedule_selector(HelloWorld::gameLogic),?1.0); |
增加gameLogic方法,代碼如下:
| 1 2 3 4 |
void?HelloWorld::gameLogic(?float?dt?) { ????this->addMonster(); } |
7.編譯運(yùn)行,可以看到右邊怪物定時增加,并且以不同的速度向左邊移動,如下圖所示:
8.接著讓玩家可以射擊子彈,當(dāng)用戶在屏幕點(diǎn)擊時,就讓玩家往點(diǎn)擊的方向進(jìn)行發(fā)送子彈,用戶的屏幕點(diǎn)擊點(diǎn)并不是子彈移動的最終地,借用原文的一張圖片來說明:
要讓層可以支持觸摸,需要在init方法,添加如下代碼:
| 1 | this->setTouchEnabled(true); |
然后重載ccTouchesEnded方法,代碼如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
void?HelloWorld::ccTouchesEnded(CCSet?*pTouches,?CCEvent?*pEvent) { ????CCTouch?*touch?=?(CCTouch*)pTouches->anyObject(); ????CCPoint?location?=?this->convertTouchToNodeSpace(touch); ????CCSize?winSize?=?CCDirector::sharedDirector()->getWinSize(); ????CCPoint?offset?=?ccpSub(location,?projectile->getPosition()); ????if?(offset.x?<=?0) ????this->addChild(projectile); ????int?realX?=?winSize.width?+?projectile->getContentSize().width?/?2; ????int?offRealX?=?realX?-?projectile->getPosition().x; ????projectile->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration,?realDest),? |
首先,得到觸摸點(diǎn),然后創(chuàng)建子彈精靈,算出觸摸點(diǎn)與子彈初始位置之差,若觸摸點(diǎn)在初始位置的前方(即玩家前方),則添加子彈到層上。以同比例方法,計(jì)算出子彈飛向屏幕右邊的最終坐標(biāo)。然后再用勾股定理計(jì)算飛行長度,假定速度為每秒480像素,則計(jì)算出飛行總時間。之后就是讓子彈執(zhí)行給定的飛行動作,以及之后的刪除自身調(diào)用。
9.編譯運(yùn)行,往屏幕點(diǎn)擊,可以看到子彈發(fā)射出去,如下圖所示:
10.當(dāng)子彈碰到怪物時,怪物被消滅,子彈消失,即碰撞檢測。需要在場景中跟蹤目標(biāo)和子彈,在HelloWorldScene.h聲明如下:
| 1 2 |
cocos2d::CCArray?*_monsters; cocos2d::CCArray?*_projectiles; |
在構(gòu)造函數(shù)和析構(gòu)函數(shù),添加如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
HelloWorld::HelloWorld() { ????_monsters?=?NULL; ????_projectiles?=?NULL; } HelloWorld::~HelloWorld() |
然后在init函數(shù)中初始化這兩個數(shù)組:
| 1 2 3 4 |
this->_monsters?=?CCArray::create(); this->_monsters->retain(); this->_projectiles?=?CCArray::create(); this->_projectiles->retain(); |
修改addMonster函數(shù),為怪物精靈添加標(biāo)簽,并加入到數(shù)組,代碼如下:
| 1 2 |
monster->setTag(1); _monsters->addObject(monster); |
修改ccTouchesEnded函數(shù),為子彈精靈添加標(biāo)簽,并加入到數(shù)組,代碼如下:
| 1 2 |
projectile->setTag(2); _projectiles->addObject(projectile); |
然后修改spriteMoveFinished函數(shù),增加如下代碼:
| 1 2 3 4 5 6 7 8 |
if?(sprite->getTag()?==?1) { ????_monsters->removeObject(sprite); } else?if?(sprite->getTag()?==?2) { ????_projectiles->removeObject(sprite); } |
添加如下方法:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
void?HelloWorld::update(float?dt) { ????CCArray?*projectilesToDelete?=?CCArray::create(); ???? ????CCObject?*pObject?=?NULL; ????CCObject?*pObject2?=?NULL;? ????CCARRAY_FOREACH(_projectiles,?pObject) ????{ ????????CCSprite?*projectile?=?(CCSprite*)pObject; ????????CCArray?*monstersToDelete?=?CCArray::create(); ????????CCARRAY_FOREACH(_monsters,?pObject2) ????????CCARRAY_FOREACH(monstersToDelete,?pObject2) ????????if?(monstersToDelete->count()?>?0) ????CCARRAY_FOREACH(projectilesToDelete,?pObject) |
遍歷子彈數(shù)組,計(jì)算每一個子彈所可能遇到的怪物,用它們各自的邊界框進(jìn)行交叉檢測,檢測到交叉,則將怪物對象放入ToDelete(待刪除)數(shù)組,不能在遍歷的時候刪除一個對象。若是子彈遇到了怪物,也需要放入ToDelete(待刪除)數(shù)組。然后從場景和數(shù)組中移動掉。同樣,也在init函數(shù),安裝定時器,代碼如下:
| 1 | this->schedule(schedule_selector(HelloWorld::update)); |
11.編譯運(yùn)行,這時當(dāng)子彈和怪物碰撞時,它們就會消失;
12.添加音效,在init函數(shù)添加背景音樂,代碼如下:
| 1 | CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav"); |
在ccTouchesEnded函數(shù),添加子彈音效,代碼如下:
| 1 | CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav"); |
13.接下來,創(chuàng)建一個新的場景,來指示"You Win"或者"You Lose"。右鍵 工程,"Add"→"Class..."→"C++"→"Add","Base class"為CCLayerColor,"Class name"為GameOverLayer,如下圖所示:
GameOverLayer.h文件代碼為:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#pragma?once #include?"cocos2d.h" class?GameOverLayer?: ????static?cocos2d::CCScene*?sceneWithWon(bool?won); |
GameOverLayer.cpp文件代碼為:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include?"GameOverLayer.h" #include?"HelloWorldScene.h" using?namespace?cocos2d; GameOverLayer::GameOverLayer(void) GameOverLayer::~GameOverLayer(void) GameOverLayer*?GameOverLayer::createWithWon(bool?won) bool?GameOverLayer::initWithWon(bool?won) ????????char?*message; ????????this->runAction(CCSequence::create(CCDelayTime::create(3),? ????return?bRet; cocos2d::CCScene*?GameOverLayer::sceneWithWon(bool?won) ????????GameOverLayer?*layer?=?GameOverLayer::createWithWon(won); ????????scene->addChild(layer); ????return?scene; void?GameOverLayer::gameOverDone() |
游戲結(jié)束時,切換到以上所建的場景,場景上的層顯示一個文本,在3秒之后返回到HelloWorld場景中。
14.最后,為游戲添加一些游戲邏輯。記錄玩家消滅怪物的數(shù)量,進(jìn)而決定該玩家輸贏。在HelloWorldScene.h文件中,添加如下:
| 1 | int?_monstersDestroyed; |
在HelloWorldScene.cpp文件,HelloWorld()構(gòu)造函數(shù),添加如下代碼:
| 1 | _monstersDestroyed?=?0; |
添加頭文件引用:
| 1 | #include?"GameOverLayer.h" |
在update定時函數(shù)中,monstersToDelete循環(huán)removeChild(monster, true)的后面添加被消滅怪物的計(jì)數(shù),并判斷勝利條件,代碼如下:
| 1 2 3 4 5 6 |
_monstersDestroyed++; if?(_monstersDestroyed?>?30) { ????CCScene?*gameOverScene?=?GameOverLayer::sceneWithWon(true); ????CCDirector::sharedDirector()->replaceScene(gameOverScene); } |
最后為玩家添加失敗條件,規(guī)定只要有一只怪物跑到左邊屏幕里,則玩家失敗,在spriteMoveFinished函數(shù)里,sprite->getTag() == 1條件的后面,添加如下:
| 1 2 |
CCScene?*gameOverScene?=?GameOverLayer::sceneWithWon(false); CCDirector::sharedDirector()->replaceScene(gameOverScene); |
14.編譯并運(yùn)行,到此已完成了一個簡單的游戲,包含音效,并帶有勝利和失敗的結(jié)束。游戲效果如下:
參考資料:
1.How To Make A Simple iPhone Game with Cocos2D 2.X Tutorial http://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2-x-tutorial
2.如何用Cocos2d來開發(fā)簡單的IPhone游戲教程 http://www.cocoachina.com/bbs/read.php?tid-15554.html
3.Cocos2d Classic Tutorial Demo Revisit:(1) http://www.zilongshanren.com/cocos2d-classic-tutorial-demo-revisit-1/
非常感謝以上資料,本例子源代碼附加資源下載地址:http://download.csdn.net/detail/akof1314/4857315
如文章存在錯誤之處,歡迎指出,以便改正。
?
???????????
給我老師的人工智能教程打call!http://blog.csdn.net/jiangjunshow
總結(jié)
以上是生活随笔為你收集整理的如何制作一个简单的游戏 Cocos2d x 2 0 4的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度开源深度学习平台Paddle
- 下一篇: EOJ Monthly 2020.9 S