获得所述路径之后。我们将能够使根据的步行路径的作用,当您点击gobutton什么时候。我们呼吁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(),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();//開始行走。先走一步,走完一步后,再走下一步
}
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]);
}

測试发现角色最终能够正常走动了

流程图例如以下

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

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

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

版权声明:本文博主原创文章,博客,未经同意不得转载。

Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色的散步路径的更多相关文章

  1. Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3

    点击下载代码   http://download.csdn.net/detail/lideguo1979/8291803 新建一个类RouteNavigation,定义getPath()方法.用来获取 ...

  2. Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_1

    以下是一些设计略显繁琐,有必要清除思维. 下一个主要的成就,当我们点击Gobutton后,得到一个随机数骰子,是走了几步,它是基于以下步骤行走路径的数目,然后移动位置的基于角色的路径. 流程如图普遍认 ...

  3. Cocos2d-x 3.2 大富翁游戏项目开发-第五部分 单机游戏-级别选择ScrollView

    于MenuScene.cpp 点击单机游戏后会调用 Director::getInstance()->pushScene(MapChooseScene::createScene()); 进入到关 ...

  4. Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_2

    在编写获取路径方法前,我们先把角色须要的动画文件载入进来,角色的文件为png 和 plist格式. player1_anim.png.plist             player1_anim.pn ...

  5. iOS开发UI篇—Quartz2D使用(绘图路径)

    iOS开发UI篇—Quartz2D使用(绘图路径) 一.绘图路径 A.简单说明 在画线的时候,方法的内部默认创建一个path.它把路径都放到了path里面去. 1.创建路径  cgmutablepat ...

  6. 使用Jquery+EasyUI 进行框架项目开发案例讲解之三---角色管理源码分享

    使用Jquery+EasyUI 进行框架项目开发案例讲解之三 角色管理源码分享    在上两篇文章  <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享> ...

  7. 网站开发进阶(四十三)html中,路径前加“/” 与不加“/”的区别

    网站开发进阶(四十三)html中,路径前加"/" 与不加"/"的区别 前言 <script src="js/downloadify.js&quo ...

  8. Qt移动应用开发(八):实现跨平台的QML和OpenGL混合渲染

    Qt移动应用开发(八):实现跨平台的QML和OpenGL混合渲染 上一篇文章讲到了利用C++这个桥梁,我们实现了QML和Java的交互.Qt 5大力推崇的QML/JS开发,让轻量.高速开发的QML/J ...

  9. Solon 开发,八、注入依赖与初始化

    Solon 开发 一.注入或手动获取配置 二.注入或手动获取Bean 三.构建一个Bean的三种方式 四.Bean 扫描的三种方式 五.切面与环绕拦截 六.提取Bean的函数进行定制开发 七.自定义注 ...

随机推荐

  1. Apache James使用的方法及相关心得(转)

    经过一番的辛苦努力,终于把James 配置搞定啦,好记性不如烂笔头啊,赶紧记下我的成功经过,以备以后查阅! 首先要做的就是配置域名的MX 记录啦: 先添加一条A记录: mail.abc.com 指向 ...

  2. POJ 36666 Making the Grade 简单DP

    题意是: 给出n个数,让你用最小的花费将其改成非递增或非递减的 然后花费就是新序列与原序列各个位置的数的差的绝对值的和 然后可以看到有2000个数,数的范围是10亿 仔细观察可以想象到.其实改变序列中 ...

  3. Android 从硬件到应用程序:一步一步爬上去 5 -- 在Frameworks蒂姆层硬件服务

    Android Frameworks层提供硬件服务,Android系统APP能够调用这些硬件服务,而硬件则完全控制.实现应有的功能.上一页下一页.为了这一个frameworks高层的应用java接口硬 ...

  4. ZOJ 2334(Monkey King-左偏树第一题)

    Monkey King Time Limit: 10 Seconds      Memory Limit: 32768 KB Once in a forest, there lived N aggre ...

  5. Outlook将收到邮件的附件保存在磁盘

    1. 新建一个宏 1)文件->选项->自定义功能区, 把主选项卡的 开发工具勾选上. 2)开发工具->宏,输入宏名,创建. 加入以下代码 Public Sub SaveAttach( ...

  6. Spark的分布式计算

    Spark,Spark是什么,如何使用Spark 1.Spark基于什么算法的分布式计算(很简单) 2.Spark与MapReduce不同在什么地方 3.Spark为什么比Hadoop灵活 4.Spa ...

  7. 重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议

    原文:重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议 [源码下载] 重新想象 Windows 8 Store ...

  8. RH033读书笔记(4)-Lab 5 File Permissions

    Lab 5 File Permissions Sequence 1: Determining File Permissions 1. What is the symbolic representati ...

  9. java多线程Future和Callable类的解释与使用

    一,描写叙述 ​在多线程下编程的时候.大家可能会遇到一种需求,就是我想在我开启的线程都结束时,同一时候获取每一个线程中返回的数据然后再做统一处理,在这种需求下,Future与Callable的组合就派 ...

  10. 开展:随笔记录 OSGI的jar增加了一些小问题和注意事项

    在引用jar当包,假设引用的项目包.在需要MANIFEST.MF 它定义 一.外用jar: 实例:外部参考需要包装的Import package里面 定义一下.如:google-gson-2.2.2. ...