昨天收到了電子工業(yè)出版社寄過來的《cocos2d-x游戲開發(fā)之旅》這本書了,書還是不錯(cuò)的,那天rp爆發(fā)在微博上抽到的獎(jiǎng)品。
感覺自己這個(gè)系列寫的好像有點(diǎn)慢,但是想說盡可能把每一個(gè)點(diǎn)介紹到,所以,嫌啰嗦的請(qǐng)見諒咯。。。
我在這個(gè)游戲中對(duì)精靈和層的處理方式是一個(gè)層中只放一種精靈,把精靈的接口提供出去,然后通過層的疊加來實(shí)現(xiàn)整個(gè)游戲。
飛機(jī)是Hero,所以它應(yīng)該是一個(gè)單例類,cocos2d-x中有很多單例類,成員函數(shù)中有sharedxxxx的基本上就是。比如導(dǎo)演類,幾個(gè)cache類等等。但是我這里偷個(gè)懶,并沒有把它處理成單例類,因?yàn)檎麄€(gè)游戲結(jié)構(gòu)比較簡單,但是在大的項(xiàng)目中就不能這么處理了。
和導(dǎo)演類的shareDirector一樣,我們?cè)赑laneLayer中也添加PlaneLayer的一個(gè)靜態(tài)指針sharedPlane。但是和導(dǎo)演類不一樣的是,必須在創(chuàng)建PlaneLayer后才能使用這個(gè)sharedPlane,而且在PlaneLayer銷毀后就不能使用它,這里需要我們自己控制好整個(gè)流程。
//PlaneLayer.h
class PlaneLayer :
public CCLayer
{
public:
PlaneLayer(void);
~PlaneLayer(void);
static PlaneLayer* create();//實(shí)現(xiàn)create函數(shù)
virtual bool init();
public:
static PlaneLayer* sharedPlane;//提供sharedPlane全局指針
};
//PlaneLayer.cpp
PlaneLayer* PlaneLayer::sharedPlane=NULL;//靜態(tài)變量要在cpp外初始化
PlaneLayer* PlaneLayer::create()
{
PlaneLayer *pRet = new PlaneLayer();
if (pRet && pRet->init())
{
pRet->autorelease();
sharedPlane=pRet;//獲得靜態(tài)指針sharedPlane的值
return pRet;
}
else
{
CC_SAFE_DELETE(pRet);
return NULL;
}
}
和示例不一樣,這里的create我們要自己實(shí)現(xiàn),而不能簡單的使用LAYER_CREATE_FUNC宏了。
而這個(gè)層的使用,只需要在上一篇中的GameLayer.cpp的init函數(shù)里調(diào)用就可以了。
//加入planeLayer
this->planeLayer=PlaneLayer::create();
this->addChild(planeLayer);
細(xì)心的玩家會(huì)發(fā)現(xiàn),飛機(jī)在飛行過程中機(jī)尾是在噴火的,其實(shí)這就是幀動(dòng)畫。
這里篇幅有限,就簡單介紹下幀動(dòng)畫和這里用到的其他動(dòng)畫效果。
幀動(dòng)畫的使用步驟:
(1)create,創(chuàng)建CCAnimation類實(shí)例。
(2)setDelayPerUnit,設(shè)置幀間間隔時(shí)間。
(3)addSpriteFrame,添加幀圖片。
(4)create,創(chuàng)建CCAnimate類實(shí)例,傳入(1)的CCAnimation實(shí)例,注意CCAnimation是動(dòng)畫過程是名詞,CCAnimate才是動(dòng)畫動(dòng)作。
(5)精靈調(diào)用runAction即可使用。
(6)注意CCAnimationCache這也是一個(gè)動(dòng)畫類全局緩沖池。如果大量重復(fù)動(dòng)畫可以放里面使用,加快游戲速度。
bool PlaneLayer::init()
{
bool bRet=false;
do
{
CC_BREAK_IF(!CCLayer::init());
CCSize winSize=CCDirector::sharedDirector()->getWinSize();
//創(chuàng)建飛機(jī)精靈前要先調(diào)用CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("shoot.plist");加載全局資源
//我把這個(gè)調(diào)用plist放到welcome.cpp中了。不然plane會(huì)空指針。
CCSprite* plane=CCSprite::create(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero1.png"));
plane->setPosition(ccp(winSize.width/2,plane->getContentSize().height/2));//飛機(jī)放置在底部中央
this->addChild(plane,0,AIRPLANE);//添加精靈,AIRPLANE是tag
CCBlink *blink=CCBlink::create(1,3);//閃爍動(dòng)畫
CCAnimation* animation=CCAnimation::create();
animation->setDelayPerUnit(0.1f);
animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero1.png"));
animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("hero2.png"));
CCAnimate* animate=CCAnimate::create(animation);//幀動(dòng)畫
plane->runAction(blink);//執(zhí)行閃爍動(dòng)畫
plane->runAction(CCRepeatForever::create(animate));// 執(zhí)行幀動(dòng)畫
bRet=true;
} while (0);
return bRet;
}
那這些CCBlink,CCRepeaterForever又是什么?
這就是cocos2d-x提供的一些動(dòng)畫效果,其實(shí)用起來都很簡單,無非是設(shè)置duration持續(xù)時(shí)長,repeat重復(fù)次數(shù),和一些位移的坐標(biāo)點(diǎn),角度等等,就可以直接運(yùn)行??梢栽赾ocos2d-x的test示例中找到。
給GameLayer層添加成員變量PlaneLayer型指針,在GameLayer::init()中調(diào)用PlaneLayer的create函數(shù)初始化飛機(jī)層。
bool GameLayer::init()
{
bool bRet=false;
do
{
...省略代碼
//加入planeLayer
this->planeLayer=PlaneLayer::create();
this->addChild(planeLayer);
...省略代碼
bRet=true;
} while (0);
return bRet;
}
(1)成員變量bool型isAlive,作為判斷飛機(jī)主角是否活著的標(biāo)志,初始為true。
(2)成員變量int型score,作為分?jǐn)?shù),出師為0,最高為20億。
(3)void MoveTo(CCPoint location);飛機(jī)層移動(dòng)的方法。主要是用于觸摸飛機(jī)移動(dòng)。會(huì)在[第七篇:觸摸事件和優(yōu)先級(jí)][1]有介紹。
(4)void Blowup(int passScore);飛機(jī)爆炸方法。當(dāng)主角碰到炸彈死亡后執(zhí)行爆炸動(dòng)畫效果。在[第八篇:自定義敵機(jī)精靈][2]有介紹。
(5)void RemovePlane();移除飛機(jī)并轉(zhuǎn)換至GameOver場景。
以上這幾個(gè)方法和屬性在后面文章中會(huì)有介紹。
現(xiàn)在飛機(jī)出現(xiàn)了,在一開始閃爍3次以后,結(jié)合上篇的背景滾動(dòng),飛機(jī)看起來就像是在噴氣往前飛了,當(dāng)然飛機(jī)相對(duì)屏幕的位置還是不變的(底部中央)。。。吼吼。。。
效果圖
更多建議: