在購買空地時,我們增加動畫效果:
該動畫包含2部分,第一部分是讓腳印由小變大,再由大變小,第二部分是腳印變小後,播放一個粒子效果。
首先下載粒子編輯工具:Cocos2d-x-ParticleEditor這是一個開源免費的工具,下載地址爲:
https://github.com/chaseli/Cocos2d-x-ParticleEditor-for-Windows
打開bin目錄下的ParticleEditor.exe 開啓工具,裏面包含了粒子實例,我們可以從中選取部分粒子效果爲我所用,也可以自己編輯效果,總之採用這個工具編輯粒子非常方便。大家可以邊修改右邊的數據邊瀏覽效果。
我們開始修改一下我們的代碼,把動作和粒子效果添加進來
1、 首先修改一下RicherGameController部分代碼
<span style="color:#333333;">void RicherGameController::endGo()
{
GameBaseScene::pathMarkVector.at(stepHasGone)->setVisible(false);
stepHasGone++;
if(stepHasGone >= stepsCount)
{
//當角色走完之後,都調用handlePropEvent方法,處理道具相關事項
handlePropEvent();
return;
}
currentRow = nextRow;
currentCol = nextCol;
moveOneStep(_richerPlayer);
log("go end");
}
</span>
<span style="color:#333333;">void RicherGameController::handlePropEvent()
{
……………………………
for (int i = 0; i < 4; i++)
{
Point ptMap = Util::GL2map(Vec2(positionAroundEnd[i][0],positionAroundEnd[i][1]), GameBaseScene::_map);
int titleId = GameBaseScene::landLayer->getTileGIDAt(ptMap);
if(titleId == GameBaseScene::blank_land_tiledID)
{
…………………………
//在這裏把角色的tag 從消息中發送出去
String * str = String::createWithFormat("%d-%f-%f-%d",MSG_BUY_BLANK_TAG,x,y,_richerPlayer->getTag());
NotificationCenter::getInstance()->postNotification(MSG_BUY,str);
break;
}
}
}
</span>
2、修改GameBaseScene.cpp文件
<span style="color:#333333;">這個方法主要是把動作相關的腳印精靈加載進來,便於後期使用
void GameBaseScene::doSomeForParticle()
{
scaleby1ForBuyLand = ScaleBy::create(0.1, 1.5);
scaleby2ForBuyLand = ScaleBy::create(0.5, 0.7);
scaleby1ForBuyLand->retain();
scaleby2ForBuyLand->retain();
foot1Sprite = Sprite::create(PLAYER1_1_PARTICLE_PNG);
addChild(foot1Sprite);
foot1Sprite->setAnchorPoint(ccp(0,0));
foot2Sprite = Sprite::create(PLAYER2_1_PARTICLE_PNG);
addChild(foot2Sprite);
foot2Sprite->setAnchorPoint(ccp(0,0));
}
</span>
<span style="color:#333333;">GameBaseScene.cpp中的receivedNotificationOMsg方法根據消息分別處理
void GameBaseScene::receivedNotificationOMsg(Object* data)
{
…………………………
case MSG_BUY_BLANK_TAG:
{
buy_land_x = messageVector.at(1)->floatValue();
buy_land_y = messageVector.at(2)->floatValue();
int playerTag = messageVector.at(3)->intValue();
//當接收到前面RicherGameController 發送來的購買空地塊的消息後,根據角色分別處理
switch(playerTag)
{
case PLAYER_1_TAG: //當是主角時 ,調用showBuyLandDialog方法,彈出對話框 交由人工處理
{
showBuyLandDialog(MSG_BUY_BLANK_TAG);
break;
}
case PLAYER_2_TAG://當是第二個角色時,不會彈出對話框,直接買地,運行動畫,播放粒子效果
{
Point pointOfGL = Util::map2GL(ccp(buy_land_x,buy_land_y),GameBaseScene::_map);
foot2Sprite->setVisible(true);
foot2Sprite->setPosition(pointOfGL);
Point pointOfMap = ccp(buy_land_x,buy_land_y);
foot2Sprite->runAction(Sequence::create(scaleby1ForBuyLand, scaleby2ForBuyLand,</span>
<span style="color:#333333;"><span style="white-space:pre"> </span>CallFunc::create([this,pointOfMap,pointOfGL]()
{
playParticle(pointOfGL,PLAYER2_1_PARTICLE_PLIST);
foot2Sprite->setVisible(false);
landLayer->setTileGID(player2_building_1_tiledID,ccp(buy_land_x,buy_land_y));
NotificationCenter::getInstance()->postNotification(MSG_PICKONE_TOGO,String:<span style="white-space:pre"> </span>:createWithFormat("%d",MSG_PICKONE_TOGO_TAG));
}
),NULL));
break;
}
}
break;
}
…………………………………..
}
}
</span>
3、
接着看主角(人工)點擊對話框中的確認按鈕後,動作的執行情況
<span style="color:#333333;">void GameBaseScene::buyLandCallback(Node *pNode)
{
if(pNode->getTag() == Btn_OK_TAG)
{
switch(popDialog->getDataTag())
{
case MSG_BUY_BLANK_TAG:
{
//當點擊空白地塊購買的確認按鈕後,首先把land層地塊的座標轉換成GL座標
Point pointOfGL = Util::map2GL(ccp(buy_land_x,buy_land_y),GameBaseScene::_map);
foot1Sprite->setVisible(true);
foot1Sprite->setPosition(pointOfGL);
Point pointOfMap = ccp(buy_land_x,buy_land_y);
//讓foot 圖片順序執行放大縮小的動畫,然後調用playParticle方法,播放粒子效果
foot1Sprite->runAction(Sequence::create(scaleby1ForBuyLand, scaleby2ForBuyLand,</span>
<span style="color:#333333;"><span style="white-space:pre"> </span>CallFunc::create([this,pointOfMap,pointOfGL](){
playParticle(pointOfGL,PLAYER1_1_PARTICLE_PLIST);
//讓foot圖片隱藏,並設置land的Title的gid,更換成foot圖塊
foot1Sprite->setVisible(false);
landLayer->setTileGID(player1_building_1_tiledID,pointOfMap);
}
),NULL));
log( "need $1000 ");
break;
}
case MSG_BUY_LAND_1_TAG:
……………………..
}
popDialog->setVisible(false);
//1秒後,發送消息,讓其他角色行走
this->scheduleOnce(schedule_selector( GameBaseScene::sendMSGPickOneToGO),1.0f);
log("Btn_OK_TAG");
}
……………………………………
}
</span>
<span style="color:#333333;">//粒子播放方法,就是加載粒子的plist文件,然後添加進來,播放完畢後釋放 。
void GameBaseScene::playParticle(Point point ,char* plistName)
{
ParticleSystem* particleSystem_foot = ParticleSystemQuad::create(plistName);
particleSystem_foot->retain();
ParticleBatchNode *batch = ParticleBatchNode::createWithTexture(particleSystem_foot->getTexture());
batch->addChild(particleSystem_foot);
addChild(batch);
particleSystem_foot->setPosition( point + ccp(tiledWidth/2,tiledHeight/2));
particleSystem_foot->release();
}
</span>
效果如圖
點擊下載代碼 http://download.csdn.net/detail/lideguo1979/8315969
未完待續...................