Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色按路径行走
来源:未知 责任编辑:责任编辑 发表时间:2015-09-09 15:29 点击:次
路径获得之后,我们就可以让角色按照路径行走了,当点击go按钮的时候,我们调用player的startGo()方法,传入的参数就是保存了路径的2个一维数组
void GameBaseScene::goButtonCallback(cocos2d::CCObject *pSender) { RouteNavigation::getInstance()->getPath(player1,3,canPassGrid,tiledRowsCount,tiledColsCount); std::vectorcolVector = RouteNavigation::getInstance()->getPathCols_vector(); std::vector rowVector = RouteNavigation::getInstance()->getPathRow_vector(); for(int i=0;i startGo(rowVector,colVector); }
给类RicherPlayer添加相应的startGo方法
void RicherPlayer::startGo(std::vectorrowVector,std::vector colVector) { //获取游戏控制器RicherGameController,调用其中的startRealGo()方法,开始真正的角色行走 RicherGameController* rgController = RicherGameController::create(); addChild(rgController); rgController->startRealGo(rowVector,colVector,this); }
void RicherGameController::startRealGo(std::vectorrowVector,std::vector 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(),playerGoPerFrameTime),right_animation); } if(!AnimationCache::getInstance()->animationByName(down_animation)) { AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_down_vector(),playerGoPerFrameTime),down_animation); } if(!AnimationCache::getInstance()->animationByName(up_animation)) { AnimationCache::getInstance()->addAnimation(Animation::createWithSpriteFrames(richerPlayer->getAnim_up_vector(),playerGoPerFrameTime),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();//开始行走,先走一步,走完一步后,再走下一步 }
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>