展会信息港展会大全

Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3
来源:互联网   发布日期:2015-09-27 15:04:43   浏览:2274次  

导读: 新建一个类RouteNavigation,定义getPath()方法,用来获取角色路径,我们采用单例模式设计该类,先看该类的定义 RouteNavigation.h class Rou...

新建一个类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= newbool*[gridRowsCount];

for(int row = 0;row direction_4;

//建立canPassDirVector_temp存放当前位置上下左右可以通过的位置

std::vector canPassDirVector_temp;

int hasGoneNumber= 0;

//开始循环查找每一步的能走的行列值

while (hasGoneNumber(direction_4).swap(direction_4);

std::vector(canPassDirVector_temp).swap(canPassDirVector_temp);

}

)>;row++)>

看一下isCanGoByColRow()方法是如何判断当前位置上下左右是否可通过的。

逻辑很简单,就是根据传进来的方向,判断二维数组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按键,测试一下是获得的路径

voidGameBaseScene::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 colVector = RouteNavigation::getInstance()->getPathCols_vector();

std::vector rowVector = RouteNavigation::getInstance()->getPathRow_vector();

//打印出路径

for(int i=0;i

测试结果如图,获取路径显示当前位置可以向 左、右、上 走。

.size();i++)>

赞助本站

人工智能实验室
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港