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

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

概述点击下载代码   http://download.csdn.net/detail/lideguo1979/8291803 新建一个类RouteNavigation,定义getPath()方法,用来获取角色路径,我们采用单例模式设计该类,先看该类的定义 RouteNavigation.hclass RouteNavigation{public: static RouteNavigation

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


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

RouteNavigation.hclass 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.cppRouteNavigation::~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 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,GO_DOWN,canPassGrID_copy);		direction_4[GO_left]  = isCanGoByColRow(currentRow,GO_left,canPassGrID_copy);		direction_4[GO_RIGHT] = isCanGoByColRow(currentRow,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()方法是如何判断当前位置上下左右是否可通过的。 逻辑很简单,就是根据传进来的方向,判断二维数组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]);	}…………………	}

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




未完待续………….

总结

以上是内存溢出为你收集整理的Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3全部内容,希望文章能够帮你解决Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_3所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1024896.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存