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

新建一个类RouteNavigation,定义getPath()方法。用来获取角色路径,我们採用单例模式设计该类,先看该类的定义

RouteNavigation.h

class RouteNavigation{
public:
static RouteNavigation* routeNav_Instance; //该类静态对象
static RouteNavigation* getInstance();//获取静态对象方法
void getPath(Sprite* playerSprite,int stepsCount,bool** canPassGrid,int gridRowsCount,int gridColsCount);//定义获取路径的方法 protected:
RouteNavigation(void);
~RouteNavigation(void); };
RouteNavigation.cpp

RouteNavigation::~RouteNavigation(void)
{
routeNav_Instance = NULL;
} RouteNavigation* RouteNavigation::getInstance()
{
if(!routeNav_Instance)
{
routeNav_Instance = new RouteNavigation();
}
return routeNav_Instance;

定义好类后,開始实现getPath()方法,还记得前面的getPath流程图吧  我就按前面的流程開始编写该方法

參数说明:
playerSprite:要获取路径的角色,就是哪个角色调用getPath方法 ,就把自己传进来
stepsCount: 角色要走多少步
canPassGrid:关卡地图是否能走动的二维数组
gridRowsCount:canPassGrid数组的行数
gridColsCount:canPassGrid数组的列数 void RouteNavigation::getPath(Sprite* playerSprite,int stepsCount,bool** canPassGrid,int gridRowsCount,int gridColsCount)
{
//定义的vector一维数组,用来存放获得的路径行列 我们先清空一下
pathCols_vector.clear();
pathRow_vector.clear();
//定义的角色当前的所在行列。下一步所处的行列
int nextCol, nextRow;
int currentCol,currentRow;
//获取角色当前所处位置的坐标值
float x = playerSprite->getPositionX();
float y = playerSprite->getPositionY();
//依据角色当前的坐标值 给角色開始的行列变量赋值。就是坐标除以每行列的宽高值
currentCol = x/tiledHeight;
//我们为了让角色居中显示,以前在GameBaseScene:: addPlayer()的方法中。给角色纵向位置+ tiledHeight,此处要减掉,才干得到正确行数
currentRow = (y - tiledWidth)/tiledWidth; //定义canPassGrid_copy。接收传过来的canPassGrid二维数组里的值
bool** canPassGrid_copy = new bool*[gridRowsCount];
for(int row = 0;row<gridRowsCount;row++)
{
for(int col = 0;col<gridColsCount;col++)
{
canPassGrid_copy[row][col] = canPassGrid[row][col];
}
} //创建一维数组direction_4[] 当中的值表示当前行列位置的上下左右四个相邻位置是否可走
std::vector<bool> direction_4; //建立canPassDirVector_temp存放当前位置上下左右能够通过的位置
std::vector<int> canPassDirVector_temp; int hasGoneNumber = 0;
//開始循环查找每一步的能走的行列值
while (hasGoneNumber<stepsCount)
{
//先清空一下数组。恢复为默认值false
direction_4.clear();
for(int i=0;i<4;i++)
{
direction_4.push_back(false);
}
canPassDirVector_temp.clear();
//查找当前行列位置的上下左右四个方向,看是否能通过,并给direction_4对应位置赋值true或false
direction_4[GO_UP] = isCanGoByColRow(currentRow,currentCol,GO_UP,canPassGrid_copy);
direction_4[GO_DOWN] = isCanGoByColRow(currentRow,currentCol,GO_DOWN,canPassGrid_copy);
direction_4[GO_LEFT] = isCanGoByColRow(currentRow,currentCol,GO_LEFT,canPassGrid_copy);
direction_4[GO_RIGHT] = isCanGoByColRow(currentRow,currentCol,GO_RIGHT,canPassGrid_copy); //遍历direction_4,找到能够通过的位置,存入canPassDirVector_temp中
for(int i=0;i<4;i++)
{
if(direction_4[i])
{
canPassDirVector_temp.push_back(i);
}
} //从记录能够通过的一维数组canPassDirVector_temp中随机取一个方向
int _rand = rand()%canPassDirVector_temp.size();
//依据方向,取得下一步的行列值
switch(canPassDirVector_temp[_rand])
{
case GO_UP:
{
nextRow = currentRow - 1;
nextCol = currentCol ;
break;
} case GO_DOWN:
{
nextRow = currentRow +1;
nextCol = currentCol;
break;
}
case GO_LEFT:
{
nextRow = currentRow ;
nextCol = currentCol - 1;
break;
}
case GO_RIGHT:
{
nextRow = currentRow ;
nextCol = currentCol + 1;
break;
}
} //switch推断完方向,给下一步行列赋值之后,存入到路径数组中
pathCols_vector.push_back(nextCol);
pathRow_vector.push_back(nextRow);
//让当前所在的行列。置为false,表示已经走过,不能够再走。防止角色踱步不前
canPassGrid_copy[currentRow][currentCol] = false;
//让当前行列值指向下一个行列位置,准备从下一个位置,查找可走的路径行列
currentCol = nextCol;
currentRow = nextRow;
//步数加1。開始查找下一个可走行列
hasGoneNumber++; } //查找完路径后。进行相关变量的内存清理释放工作
CC_SAFE_DELETE(canPassGrid_copy);
direction_4.clear();
canPassDirVector_temp.clear();
std::vector<bool>(direction_4).swap(direction_4);
std::vector<int>(canPassDirVector_temp).swap(canPassDirVector_temp); }
看一下isCanGoByColRow()方法是怎样推断当前位置上下左右是否可通过的。

逻辑非常easy,就是依据传进来的方向,推断二维数组canPassGrid对应行列是否是true。假设true,表示能够通过

bool RouteNavigation::isCanGoByColRow(int row,int col,int direction,bool** canPassGrid)
{
switch(direction) {
case GO_UP:
{
return canPassGrid[row -1][col];
}
case GO_DOWN:
{
return canPassGrid[row +1][col];
}
case GO_LEFT:
{
return canPassGrid[row][col -1];
}
case GO_RIGHT:
{
return canPassGrid[row][col +1];
}
} return false;
好了 。我们改动一下go按键。測试一下是获得的路径
void GameBaseScene::addGoButton()
{
//改动了一下Go 按键 变为了menu
Menu* menu = Menu::create();
menu->setPosition(CCPointZero);
//去调用goButtonCallback方法
MenuItemImage* goMenuItemButton = MenuItemImage::create("map/go_normal.png", "map/go_press.png", this, menu_selector(GameBaseScene::goButtonCallback)); goMenuItemButton->setPosition(ccp(tableStartPosition_x+2*tableWidth,tableStartPosition_y-tableHeight*6));
menu->addChild(goMenuItemButton);
addChild(menu);
} void GameBaseScene::goButtonCallback(cocos2d::CCObject *pSender)
{
log("go button clicked");
//先让获取走5步的路径
RouteNavigation::getInstance()->getPath(player1,5,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]);
}
………………… }

測试结果如图,获取路径显示当前位置能够向 左、右、上 走。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlkZWd1bzE5Nzk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

未完待续………….

Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3的更多相关文章

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

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

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

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

  3. Cocos2d-x 3.2 大富翁游戏项目开发-第八部分 角色的散步路径

    获得所述路径之后.我们将能够使根据的步行路径的作用,当您点击gobutton什么时候.我们呼吁player的startGo()办法.传入的参数是保存路径2一维数组 void GameBaseScene ...

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

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

  5. 微信小程序开发(七)获取手机网络类型

    // succ.wxml <view>手机网络状态:{{netWorkType}}</view> // succ.js var app = getApp() Page({ da ...

  6. C#微信公众号开发 -- (七)自定义菜单事件之VIEW及网页(OAuth2.0)授权

    通俗来讲VIEW其实就是我们在C#中常用的a标签,可以直接在自定义菜单URL的属性里面写上需要跳转的链接,也即为单纯的跳转. 但更多的情况下,我们是想通过VIEW来进入指定的页面并进行操作. 举一个简 ...

  7. 云平台编程与开发(七)-使用X5Cloud云平台开发网络彩讯

    云平台编程与开发(七)-使用X5Cloud云平台开发网络彩讯 博客分类: 云平台 云计算 Java Android Android 云平台 Java 网络彩讯定义以及工作大概流程  下载试用地址:ap ...

  8. C#的百度地图开发(三)依据坐标获取位置、商圈及周边信息

    原文:C#的百度地图开发(三)依据坐标获取位置.商圈及周边信息 我们得到了百度坐标,现在依据这一坐标来获取相应的信息.下面是相应的代码 public class BaiduMap { /// < ...

  9. 转:微信开发之使用java获取签名signature(贴源码,附工程)

    微信开发之使用java获取签名signature(贴源码,附工程) 标签: 微信signature获取签名 2015-12-29 22:15 6954人阅读 评论(3) 收藏 举报  分类: 微信开发 ...

随机推荐

  1. 为什么cp很多小文件非常慢——对cp和rm命令的一些思考

    linux中的文件复制命令——CP linux中文件剪切的命令——MV 1.问题背景 今天在某个目的动作过程中想把一个文件夹下的文件复制到另外的一个文件夹下 cp -fr   ./dir1/   /d ...

  2. 一些指令 & 一些知识 (Linux Spring log4j...)

    #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数 ...

  3. python Unable to find vcvarsall.bat 错误

    今天遇到了这个方面的问题,目前找到两种办法.一种是换编译器如mingw,另一种是装vc.第一种方法没成功,现在正在等第二种. 第一种: 首先安装MinGW: 把MinGW的路径添加到环境变量path中 ...

  4. python中的有趣用法

    本文给除了python中几个有趣的用法,可以给我们不一样的启发 1: Python中模拟使用C++ 中的   cout << import sys  class ostream: def  ...

  5. 用C语言写一个程序,得出当前系统的整形数字长(16位,32位,64位)等,不能使用sizeof()

    #include <iostream>#include <cmath>using namespace std; int main(){ int num = -1; unsign ...

  6. BZOJ 1618: [Usaco2008 Nov]Buying Hay 购买干草

    题目 1618: [Usaco2008 Nov]Buying Hay 购买干草 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 679  Solved:  ...

  7. jQuery报错:Uncaught ReferenceError: $ is not defined

    在使用jQuery的时候,发现有如下报错: Uncaught ReferenceError: $ is not defined  (anonymous function) 出现这个报错的原因: 1.j ...

  8. Android 开机动画源码分析

    Android系统在启动SystemServer进程时,通过两个阶段来启动系统所有服务,在第一阶段启动本地服务,如SurfaceFlinger,SensorService等,在第二阶段则启动一系列的J ...

  9. unix ls命令

    [语法]: ls  [-RadCxmlnogrtucpFbqisf1]   [文件夹或文件......] [说明]: ls 命令列出指定文件夹下的文件,缺省文件夹为当前文件夹 ./,缺省输出顺序为纵向 ...

  10. C# 继承细节

    假定没有为类定义任何显式的构造函数,这样编译器就会为所有的类提供默认的构造函数,在后台会进行许多操作,编译器可以很好地解决层次结构中的所有问题,每个类中的每个字段都会初始化为默认值.但在添加了一个我们 ...