Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色按路径行走(2)
来源:未知 责任编辑:责任编辑 发表时间:2015-09-09 15:29 点击:次
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,ccp(0,-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]); }
测试发现角色终于可以正常走动了
流程图如下
目前为止,代码写的相对较多了,有必要重新整理一下,下部分,我们进行一下代码优化。便于后期的继续开发。
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>