Cocos2d-x 3.2 大富翁游戏项目开发-第十一部分 提升角色行走体验

角色在获取随机步数走之前需要友好的提示玩家是哪个角色开始行走,要走几步。

这样我们在行走之前让角色闪一下,并把角色要走的路径用特殊颜色标注一下。效果如下



首先实现角色闪动:闪动是角色相关的,所以我们在RicherPlayer类的startGo方法中修改最合适

voidRicherPlayer::startGo(std::vector<int>rowVector,std::vector<int>colVector)
{
	//创建淡入淡出的动作,让角色运行,动作结束后,调用startRealGo开始行走。此处用了闭包函数简化代码
	FadeOut* fadeout = FadeOut::create(0.2);
	FadeIn* fadein = FadeIn::create(0.2);
	this->runAction(Sequence::create(fadeout,fadein,CallFunc::create([rowVector,colVector,this](){
			RicherGameController* instance =RicherGameController::getInstance();
			instance->startRealGo(rowVector,colVector,this);
		}
		),NULL));
	
}
</span>

下一步开始给路径标注颜色,我们用6个sprite来做,把这6个sprite 放入Vector中,每次根据行走步数,从vector取出sprite,放置在路径位置,每次走完这个路径位置,把该sprite的visible属性设置为false 不可见。逻辑清楚了代码写起来也很简单

GameBaseScene.h 头文件如下:

class GameBaseScene: public Layer
{
public:
	
            ………………..
	static Vector<Sprite*> pathMarkVector;//定义Vector 用来存放6个路径遮挡精灵
	static void drawPathColor(std::vector<int> rowVector,std::vector<int> colVector);//绘制路径遮挡方法
private:
       void addPathMark();//定义添加遮挡精灵的方法
       …………………….
}


cpp文件
bool GameBaseScene::init()
{
………………..
	//添加遮挡的方法一定要在添加角色方法之前,防止角色也被遮挡住
	addPathMark();
	addPlayer();
……………………..
	
	return true;
}


在GameBaseScene.cpp中实现这个addPathMark方法

void GameBaseScene::addPathMark()
{
	//这个方法就是创建6个路径遮挡Sprite,并添加到当前场景中
	Sprite* mark1 = Sprite::create(PATH_MARK_1);
	Sprite* mark2 = Sprite::create(PATH_MARK_2);
	Sprite* mark3 = Sprite::create(PATH_MARK_3);
	Sprite* mark4 = Sprite::create(PATH_MARK_4);
	Sprite* mark5 = Sprite::create(PATH_MARK_5);
	Sprite* mark6 = Sprite::create(PATH_MARK_6);

	mark1->setAnchorPoint(ccp(0,0));
	mark2->setAnchorPoint(ccp(0,0));
	mark3->setAnchorPoint(ccp(0,0));
	mark4->setAnchorPoint(ccp(0,0));
	mark5->setAnchorPoint(ccp(0,0));
	mark6->setAnchorPoint(ccp(0,0));
	//都为不可见
	mark1->setVisible(false);
	mark2->setVisible(false);
	mark3->setVisible(false);
	mark4->setVisible(false);
	mark5->setVisible(false);
	mark6->setVisible(false);

	addChild(mark1);
	addChild(mark2);
	addChild(mark3);
	addChild(mark4);
	addChild(mark5);
	addChild(mark6);

	pathMarkVector.pushBack(mark1);
	pathMarkVector.pushBack(mark2);
	pathMarkVector.pushBack(mark3);
	pathMarkVector.pushBack(mark4);
	pathMarkVector.pushBack(mark5);
	pathMarkVector.pushBack(mark6);
}


//绘制路径遮挡方法
void GameBaseScene::drawPathColor(std::vector<int> rowVector,std::vector<int> colVector)
{
//取得要绘制遮挡数量,因为路径数组0的位置是角色当前位置,不应绘制遮挡
	int stepsCount = rowVector.size()-1; 
//从遮挡容器中取出遮挡Sprite,放置到对应位置,并设为可见
	for(int i=1;i<rowVector.size();i++)
	{
		pathMarkVector.at(i-1)->setPosition(ccp(colVector[i]*32,rowVector[i]*32));
		pathMarkVector.at(i-1)->setVisible(true);
	}
}


在RicherPlayer::startGo方法中在真正开始行走之前,画出路径遮挡效果

void RicherPlayer::startGo(std::vector<int> rowVector,std::vector<int> colVector)
{
	//Blink* blink = Blink::create(0.5,1);
	FadeOut* fadeout = FadeOut::create(0.2);
	FadeIn* fadein = FadeIn::create(0.2);

	//画出路径遮挡效果
	GameBaseScene::drawPathColor(rowVector,colVector);

	this->runAction(Sequence::create(fadeout,fadein,CallFunc::create([rowVector,colVector,this](){
			RicherGameController* instance =RicherGameController::getInstance();
			instance->startRealGo(rowVector,colVector,this);
		}
		),NULL));
	
}


void RicherGameController::endGo()

{
	//在每次移动结束后,把当前路径遮挡效果设为不可见,这样就出现了每走完一步,当前路径遮挡消失的效果
	GameBaseScene::pathMarkVector.at(stepHasGone)->setVisible(false);
	stepHasGone++;

	………………

	log("go end");
	
}


点击下载代码  http://download.csdn.net/detail/lideguo1979/8305165


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

你可能感兴趣的:(cocos2d-x)