码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d-x 3.2 大富翁游戏项目开发-第十四部分 购买空地动画

时间:2014-12-31 06:23:04      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x

在购买空地时,我们增加动画效果:

该动画包含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


未完待续...................

Cocos2d-x 3.2 大富翁游戏项目开发-第十四部分 购买空地动画

标签:cocos2d-x

原文地址:http://blog.csdn.net/lideguo1979/article/details/42283507

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!