Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色按路径行走

Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色按路径行走,第1张

概述路径获得之后,我们就可以让角色按照路径行走了,当点击go按钮的时候,我们调用player的startGo()方法,传入的参数就是保存了路径的2个一维数组 void GameBaseScene::goButtonCallback(cocos2d::CCObject *pSender){ RouteNavigation::getInstance()->getPath(player1,3,canPa

路径获得之后,我们就可以让角色按照路径行走了,当点击go按钮的时候,我们调用player的startGo()方法,传入的参数就是保存了路径的2个一维数组

voID GameBaseScene::gobuttonCallback(cocos2d::CCObject *pSender){	RouteNavigation::getInstance()->getPath(player1,3,canPassGrID,tiledRowsCount,tiledColsCount);	std::vector<int> colVector = RouteNavigation::getInstance()->getPathCols_vector();	std::vector<int> rowVector = RouteNavigation::getInstance()->getPathRow_vector();	for(int i=0;i<rowVector.size();i++)	{		log(" rowVector row is %d --- colVector col is %d",rowVector[i],colVector[i]);	}	//调用RicherPlayer类的startGo方法	player1->startGo(rowVector,colVector);}


给类RicherPlayer添加相应的startGo方法

voID RicherPlayer::startGo(std::vector<int> rowVector,std::vector<int> colVector){	//获取游戏控制器RicherGameController,调用其中的startRealGo()方法,开始真正的角色行走	RicherGameController* rgController = RicherGameController::create();	addChild(rgController);	rgController->startRealGo(rowVector,colVector,this);}

voID RicherGameController::startRealGo(std::vector<int> rowVector,std::vector<int> colVector,RicherPlayer* richerPlayer){	currentRow = rowVector[0];	currentCol = colVector[0]; //获取第一个位置的行列值	nextRow =0;	nextCol =0; //下一步的行列值	//创建上下左右的动作,并放入缓存	if(!AnimationCache::getInstance()->animationByname("left_animation"))	{AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_left_vector(),playerGoPerFrameTime),"left_animation");	}	if(!AnimationCache::getInstance()->animationByname("right_animation"))	{AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_right_vector(),"right_animation");	}	if(!AnimationCache::getInstance()->animationByname("down_animation"))	{AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_down_vector(),"down_animation");	}	if(!AnimationCache::getInstance()->animationByname("up_animation"))	{AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_up_vector(),"up_animation");	}	//从缓存中取得上下左右的动作,创建相应的动画	 left = Animate::create(AnimationCache::getInstance()->animationByname("left_animation"));	 right =Animate::create( AnimationCache::getInstance()->animationByname("right_animation"));	 down =Animate::create(AnimationCache::getInstance()->animationByname("down_animation"));	 up = Animate::create(AnimationCache::getInstance()->animationByname("up_animation"));	//retain 一下,引用计数加一,防止动画被清除	 left->retain();	 right ->retain();	 down->retain();	 up->retain();	//根据参数给相应变量赋值	_rowVector=rowVector;	_colVector=colVector;	_richerPlayer =richerPlayer;	stepHasGone = 0;//角色已经走了几步	stepsCount = _rowVector.size()-1;//取得路径需要走的步数,因为第一个是当前位置的行列,所以不计入步数	moveOnestep();//开始行走,先走一步,走完一步后,再走下一步}

voID RicherGameController::moveOnestep(){		//获取下一步行列,计算同当前行列的差值		nextRow = _rowVector[stepHasGone+1];	                nextCol = _colVector[stepHasGone+1];		int distanceRow = nextRow - currentRow;	                int distanceCol = nextCol - currentCol;		MoveBy* moveBy;	Repeat* repeate;	Action* spawnAction;		//根据行列的差值,创建上下左右相应的动作,包括移动和行走的动画		if(distanceRow >0)//up		{			moveBy = MoveBy::create(playerGoTotalTime,ccp(0,tiledHeight)); 			repeate = Repeat::create(up,1);		}		if(distanceRow <0)//down		{			moveBy = MoveBy::create(playerGoTotalTime,-tiledHeight)); 			repeate = Repeat::create(down,1);		}		if(distanceCol >0)//right		{			moveBy = MoveBy::create(playerGoTotalTime,ccp(tileDWIDth,0)); 			repeate = Repeat::create(right,1);		}		if(distanceCol <0)//left		{			moveBy = MoveBy::create(playerGoTotalTime,ccp(-tileDWIDth,0)); 			repeate = Repeat::create(left,1);		}		//创建同步动画,当移动完毕,执行callEndGoFunc方法,进而调用endGo()方法		spawnAction = Sequence::create(Spawn::create(moveBy,repeate,NulL),callEndGoFunc,NulL);		_richerPlayer->runAction(spawnAction);}

voID RicherGameController::endGo(){	stepHasGone++;//走完一步后,已走步数加1	if(stepHasGone >= stepsCount) //如果已走步数大于等于总步数,返回	{		return;	}	currentRow = nextRow;	currentCol = nextCol;//当前行列赋值为下一行列	moveOnestep();//开始下一步的移动	log("go end");}


经过测试发现,角色会来回走动,走过去了还走回来,所以我们需要给角色类Player添加 表示角色从哪个位置过来的属性

修改RouteNavigation类的getPath()方法

voID RouteNavigation::getPath(RicherPlayer* player,int stepsCount,bool** canPassGrID,int grIDRowsCount,int grIDColsCount){…………………………..	int rowtemp = player->getComeFromeRow();	int coltemp = player->getComeFromCol();	if(rowtemp <=-1 || coltemp <= -1)	{		player->setComeFromCol(currentCol);		player->setComeFromeRow(currentRow);	}	//设置角色从哪里来的位置为false ,以表示不可通过	canPassGrID_copy[player->getComeFromeRow()][player->getComeFromCol()] = false;………………………..	//获取完路径后,设置角色的来自的位置	player->setComeFromCol(pathCols_vector[pathCols_vector.size()-2]);	player->setComeFromeRow(pathRow_vector[pathRow_vector.size()-2]);}

测试发现角色终于可以正常走动了




流程图如下


目前为止,代码写的相对较多了,有必要重新整理一下,下部分,我们进行一下代码优化。便于后期的继续开发


点击下载代码 http://download.csdn.net/detail/lIDeguo1979/8292407

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

总结

以上是内存溢出为你收集整理的Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色按路径行走全部内容,希望文章能够帮你解决Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色按路径行走所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1024958.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存