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

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色按路径行走前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

  1. void GameBaseScene::goButtonCallback(cocos2d::CCObject *pSender)
  2. {
  3. RouteNavigation::getInstance()->getPath(player1,3,canPassGrid,tiledRowsCount,tiledColsCount);
  4. std::vector<int> colVector = RouteNavigation::getInstance()->getPathCols_vector();
  5. std::vector<int> rowVector = RouteNavigation::getInstance()->getPathRow_vector();
  6. for(int i=0;i<rowVector.size();i++)
  7. {
  8. log(" rowVector row is %d --- colVector col is %d",rowVector[i],colVector[i]);
  9. }
  10. //调用RicherPlayer类的startGo方法
  11. player1->startGo(rowVector,colVector);
  12. }


给类RicherPlayer添加相应的startGo方法

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

  1. void RicherGameController::startRealGo(std::vector<int> rowVector,std::vector<int> colVector,RicherPlayer* richerPlayer)
  2. {
  3. currentRow = rowVector[0]; currentCol = colVector[0]; //获取第一个位置的行列值
  4. nextRow =0; nextCol =0; //下一步的行列值
  5. //创建上下左右的动作,并放入缓存
  6. if(!AnimationCache::getInstance()->animationByName("left_animation"))
  7. {
  8. AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_left_vector(),playerGoPerFrameTime),"left_animation");
  9. }
  10. if(!AnimationCache::getInstance()->animationByName("right_animation"))
  11. {
  12. AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_right_vector(),"right_animation");
  13. }
  14. if(!AnimationCache::getInstance()->animationByName("down_animation"))
  15. {
  16. AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_down_vector(),"down_animation");
  17. }
  18. if(!AnimationCache::getInstance()->animationByName("up_animation"))
  19. {
  20. AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_up_vector(),"up_animation");
  21. }
  22. //从缓存中取得上下左右的动作,创建相应的动画
  23. left = Animate::create(AnimationCache::getInstance()->animationByName("left_animation"));
  24. right =Animate::create( AnimationCache::getInstance()->animationByName("right_animation"));
  25. down =Animate::create(AnimationCache::getInstance()->animationByName("down_animation"));
  26. up = Animate::create(AnimationCache::getInstance()->animationByName("up_animation"));
  27. //retain 一下,引用计数加一,防止动画被清除
  28. left->retain();
  29. right ->retain();
  30. down->retain();
  31. up->retain();
  32. //根据参数给相应变量赋值
  33. _rowVector=rowVector;
  34. _colVector=colVector;
  35. _richerPlayer =richerPlayer;
  36. stepHasGone = 0;//角色已经走了几步
  37. stepsCount = _rowVector.size()-1;//取得路径需要走的步数,因为第一个是当前位置的行列,所以不计入步数
  38. moveOneStep();//开始行走,先走一步,走完一步后,再走下一步
  39. }

  1. void RicherGameController::moveOneStep()
  2. {
  3. //获取下一步行列,计算同当前行列的差值
  4. nextRow = _rowVector[stepHasGone+1];
  5. nextCol = _colVector[stepHasGone+1];
  6. int distanceRow = nextRow - currentRow;
  7. int distanceCol = nextCol - currentCol;
  8.  
  9. MoveBy* moveBy; Repeat* repeate; Action* spawnAction;
  10. //根据行列的差值,创建上下左右相应的动作,包括移动和行走的动画
  11.  
  12. if(distanceRow >0)//up
  13. {
  14. moveBy = MoveBy::create(playerGoTotalTime,ccp(0,tiledHeight));
  15. repeate = Repeat::create(up,1);
  16. }
  17. if(distanceRow <0)//down
  18. {
  19. moveBy = MoveBy::create(playerGoTotalTime,-tiledHeight));
  20. repeate = Repeat::create(down,1);
  21. }
  22. if(distanceCol >0)//right
  23. {
  24. moveBy = MoveBy::create(playerGoTotalTime,ccp(tiledWidth,0));
  25. repeate = Repeat::create(right,1);
  26. }
  27. if(distanceCol <0)//left
  28. {
  29. moveBy = MoveBy::create(playerGoTotalTime,ccp(-tiledWidth,0));
  30. repeate = Repeat::create(left,1);
  31. }
  32. //创建同步动画,当移动完毕,执行callEndGoFunc方法,进而调用endGo()方法
  33. spawnAction = Sequence::create(Spawn::create(moveBy,repeate,NULL),callEndGoFunc,NULL);
  34. _richerPlayer->runAction(spawnAction);
  35. }

  1. void RicherGameController::endGo()
  2. {
  3. stepHasGone++;//走完一步后,已走步数加1
  4. if(stepHasGone >= stepsCount) //如果已走步数大于等于总步数,返回
  5. {
  6. return;
  7. }
  8. currentRow = nextRow;
  9. currentCol = nextCol;//当前行列赋值为下一行列
  10. moveOneStep();//开始下一步的移动
  11. log("go end");
  12. }


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

修改RouteNavigation类的getPath()方法

  1. void RouteNavigation::getPath(RicherPlayer* player,int stepsCount,bool** canPassGrid,int gridRowsCount,int gridColsCount)
  2. {…………………………..
  3. int rowtemp = player->getComeFromeRow();
  4. int coltemp = player->getComeFromCol();
  5. if(rowtemp <=-1 || coltemp <= -1)
  6. {
  7. player->setComeFromCol(currentCol);
  8. player->setComeFromeRow(currentRow);
  9. }
  10. //设置角色从哪里来的位置为false ,以表示不可通过
  11. canPassGrid_copy[player->getComeFromeRow()][player->getComeFromCol()] = false;
  12. ………………………..
  13. //获取完路径后,设置角色的来自的位置
  14. player->setComeFromCol(pathCols_vector[pathCols_vector.size()-2]);
  15. player->setComeFromeRow(pathRow_vector[pathRow_vector.size()-2]);
  16. }

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




流程图如下


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


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

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

猜你在找的Cocos2d-x相关文章