cocos2d-x的A*寻路
如果你还不熟悉A*寻路,请先看下这篇文章http://blog.csdn.net/dssdss123/article/details/11494065
一、先介绍几个函数和结构:
1、virtual void draw()
这个函数跟与MFC上单文档里的OnDraw函数很像,这里只是少了dc,这个函数会一直被调用,无需刷新,也就是说,你无需像在MFC上一样调用Invalidate或者InvalidateRect
2、virtual void ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent)
这个函数是下响应触摸的,当你点击屏幕时,就会进到这个函数。要使这个函数有效,你需要在init中调用
setTouchEnabled(true); // 允许该层响应触摸
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false); // 注册单点触摸
在这个例子中,我们不需要多点触摸
3、ccColor4F结构
这个结构在ccDrawSolidRect函数中将会使用到,ccDrawSolidRect是画某种颜色的矩形,对应在MFC中,我们使用的是FillSolidRect。ccColor4F是RGBA的结构,RGB是三颜色,红绿蓝,最后一个alpha值,他表示这个颜色的透明度。为1是完全不透明,0时则完全透明。
二、实现
1、去掉coco自带的乱七八糟的显示
1)去掉帧频显示
在函数bool AppDelegate::applicationDidFinishLaunching()中
// turn on display FPS
//pDirector->setDisplayStats(true);
将pDirector->setDisplayStats(true),注释掉
2)去掉menu,Hello World文字标签
在函数void HelloWorld::init()中
// Add the menu to HelloWorld layer as a child layer.
//this->addChild(pMenu, 1);
// Add the label to HelloWorld layer as a child layer.
//this->addChild(pLabel, 1);
将menu和label注释掉
3)替换背景图,并置于最底层
// 3. Add add a splash screen, show the cocos2d splash image.
CCSprite* pSprite = CCSprite::create("map.jpg"); // 将原先的HelloWorld.png,替换为自己的图片,这里我换成map.jpg
CC_BREAK_IF(! pSprite);
2、初始化地图
声明结构表示格子掩码等一些信息,我们的例子中,只需掩码,所以结构如下:
- struct ST_GRID
- {
- ST_GRID() { gf = GRID_FLAG_DEFAULT; }
- GRID_FLAG gf;
- };
- typedef vector<ST_GRID*> VEC_GRID;
- VEC_GRID m_vecGrid; // 保存地图产生的所有格子
初始化地图的格子掩码,如下:
- void HelloWorld::InitMap()
- {
- // 初始化格子掩码
- srand((unsigned int)time(NULL));
- for (int i = 0; i < GetRow() * GetCol(); i++)
- {
- int nRandFlag = ((int)(CCRANDOM_0_1() * 10)) % 4 == 0 ? GRID_FLAG_OBSTACLE : GRID_FLAG_DEFAULT; // 十分之四的概率产生障碍
- ST_GRID* pGrid = new ST_GRID;
- if (!pGrid)
- {
- return ;
- }
- pGrid->gf = (GRID_FLAG)nRandFlag;
- m_vecGrid.push_back(pGrid);
- }
- }
3、寻路
定义一个结构,用于寻路过程中,记录每个格子的信息
- struct NODE
- {
- NODE() {nIndex = 0; nG = 0; pParent = NULL;}
- int nIndex;
- int nG;
- NODE* pParent;
- };
- vector<NODE*> m_vecPath; // 寻路的路径
下面开始寻路
- void HelloWorld::FindPath()
- {
- vector<NODE*> vecClose; // close表
- vector<NODE*> vecOpen; // open表
- if (m_nStartIndex == -1 || m_nEndIndex == -1)
- {
- return ;
- }
- m_vecPath.clear(); // 这里,我们并没有delete,但却不会内存泄漏,因为cocos2d-x使用了跟java一样的技术 -- 内存回收机制,自动处理垃圾
- // 先添加开始点
- NODE* pNode = new NODE;
- pNode->nIndex = m_nStartIndex;
- vecClose.push_back(pNode);
- int nStep = 0;
- while(true)
- {
- if (nStep++ >= 200) // 最多寻200格
- {
- break;
- }
- NODE* pNextNode = vecClose[vecClose.size() - 1]; // 取下一个路径
- if (!pNextNode)
- {
- break;
- }
- if (pNextNode->nIndex == m_nEndIndex) // 找到终点,就不再找了
- {
- break;
- }
- for (int i = 0; i < 8; i++)
- {
- int nIndex = GetIndexByDir(pNextNode->nIndex, i); // 根据方向取索引
- if (-1 == nIndex)
- {
- continue;
- }
- if (m_vecGrid[nIndex]->gf == GRID_FLAG_OBSTACLE) // 障碍
- {
- continue;
- }
- if (InTable(nIndex, vecClose) != NULL) // 在close表里
- {
- continue;
- }
- NODE* pNode = InTable(nIndex, vecOpen); // 在open表里,比较G值,取G值更小的为新路径
- if (pNode)
- {
- int nNewG = pNextNode->nG + GetGByIndex(pNextNode->nIndex, pNode->nIndex);
- if (pNode->nG > nNewG)
- {
- pNode->nG = nNewG;
- pNode->pParent = pNextNode; // 改变节点的父节点
- }
- continue;
- }
- // 新搜索到的格子,添加到开放列表
- pNode = new NODE;
- pNode->nIndex = nIndex;
- pNode->nG = pNextNode->nG + GetGByIndex(pNextNode->nIndex, pNode->nIndex);
- pNode->pParent = pNextNode;
- vecOpen.push_back(pNode);
- }
- // 找下一个路径,open表里F值最小的就是了
- int nMinF = 0xFFFFFF;
- pNextNode = NULL;
- int nNextNodeIndex = 0;
- for (int i = 0; i < (int)vecOpen.size(); i++)
- {
- NODE* pNode = vecOpen[i];
- if (!pNode)
- {
- continue;
- }
- int nH = GetHByIndex(pNode->nIndex); // 计算该点与终点的H值,即路径长度
- int nF = nH + pNode->nG; // F = H + G
- if (nF < nMinF)
- {
- nMinF = nF;
- pNextNode = pNode;
- nNextNodeIndex = i;
- }
- }
- // 找到F值最小的,放入close表,并从open表里删除
- if (nNextNodeIndex >= 0 && nNextNodeIndex < (int)vecOpen.size())
- {
- vecClose.push_back(pNextNode);
- vecOpen.erase(vecOpen.begin() + nNextNodeIndex);
- }
- }
- // 寻路结束,找最优路径
- pNode = vecClose[vecClose.size() - 1];
- while (pNode)
- {
- m_vecPath.push_back(pNode);
- pNode = pNode->pParent;
- }
- }
4、展示到界面上
- void HelloWorld::draw()
- {
- // 画背景表格
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- for (int i = 0; i < GetRow(); i++)
- {
- ccDrawLine(ccp(0, i * GRID_SIDELEN), ccp(size.width, i * GRID_SIDELEN));
- }
- for (int i = 0; i < GetCol(); i++)
- {
- ccDrawLine(ccp(i * GRID_SIDELEN, 0), ccp(i * GRID_SIDELEN, size.height));
- }
- // 画特殊格子颜色
- // 寻路得到的路径
- for (int i = 0; i < (int)m_vecPath.size(); i++)
- {
- CCPoint ptObstacleLT;
- CCPoint ptObstacleRD;
- GetRectPointByIndex(m_vecPath[i]->nIndex, ptObstacleLT, ptObstacleRD);
- ccColor4F clrObstacle = {0, 1, 1, 0};
- ccDrawSolidRect(ptObstacleLT, ptObstacleRD, clrObstacle);
- }
- // 开始点
- CCPoint ptStartLT;
- CCPoint ptStartRD;
- GetRectPointByIndex(m_nStartIndex, ptStartLT, ptStartRD);
- ccColor4F clrStart = {1, 0, 0, 1};
- ccDrawSolidRect(ptStartLT, ptStartRD, clrStart);
- // 结束点
- CCPoint ptEndLT;
- CCPoint ptEndRD;
- GetRectPointByIndex(m_nEndIndex, ptEndLT, ptEndRD);
- ccColor4F clrEnd = {0, 1, 0, 1};
- ccDrawSolidRect(ptEndLT, ptEndRD, clrEnd);
- // 障碍
- for (int i = 0; i < (int)m_vecGrid.size(); i++)
- {
- if (m_vecGrid[i]->gf == GRID_FLAG_OBSTACLE)
- {
- CCPoint ptObstacleLT;
- CCPoint ptObstacleRD;
- GetRectPointByIndex(i, ptObstacleLT, ptObstacleRD);
- ccColor4F clrObstacle = {0, 0, 1, 1};
- ccDrawSolidRect(ptObstacleLT, ptObstacleRD, clrObstacle);
- }
- }
- }
这里,我只介绍几个比较重要的函数,其他的就不赘述了,资源已上传到CSDN,但还没显示出来,等显示出来了,再把链接发到此处,有疑问的童鞋留言哈
。。。。。。。。
啊,我还是直接上源码吧
- #ifndef __HELLOWORLD_SCENE_H__
- #define __HELLOWORLD_SCENE_H__
- #include "cocos2d.h"
- #include "Box2D/Box2D.h"
- #include "SimpleAudioEngine.h"
- USING_NS_CC;
- enum STEP
- {
- STEP_DEFAULT = 0,
- STEP_STARTPOINT = 1,
- STEP_ENDPOINT = 2,
- };
- enum GRID_FLAG
- {
- GRID_FLAG_DEFAULT = 0, // 默认可通过
- GRID_FLAG_OBSTACLE = 1, // 障碍
- };
- const int GRID_SIDELEN = 20; // 不能为0
- //////////////////////////////////////////////////////////////////////////
- class HelloWorld : public cocos2d::CCLayer
- {
- public:
- // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
- virtual bool init();
- // there's no 'id' in cpp, so we recommand to return the exactly class pointer
- static cocos2d::CCScene* scene();
- // a selector callback
- void menuCloseCallback(CCObject* pSender);
- // implement the "static node()" method manually
- CREATE_FUNC(HelloWorld);
- public:
- virtual void draw();
- virtual bool ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent);
- private:
- void InitMap();
- private:
- int GetRow();
- int GetCol();
- int GetIndexByPoint(CCPoint pt);
- void GetRectPointByIndex(int nIndex, CCPoint &ptLT, CCPoint &ptRD);
- private:
- int m_nStartIndex;
- int m_nEndIndex;
- struct ST_GRID
- {
- ST_GRID() { gf = GRID_FLAG_DEFAULT; }
- GRID_FLAG gf;
- };
- typedef vector<ST_GRID*> VEC_GRID;
- VEC_GRID m_vecGrid;
- struct NODE
- {
- NODE() {nIndex = 0; nG = 0; pParent = NULL;}
- int nIndex;
- int nG;
- NODE* pParent;
- };
- vector<NODE*> m_vecPath; // 寻路的路径
- public:
- void FindPath();
- private:
- int GetIndexByDir(int nIndex, int nDir);
- int GetGByIndex(int nStartIndex, int nEndIndex);
- int GetHByIndex(int nIndex);
- NODE *InTable(int nIndex, vector<NODE*> &vecTbl);
- private:
- int m_nStep;
- };
- #endif // __HELLOWORLD_SCENE_H__
- #include "HelloWorldScene.h"
- using namespace cocos2d;
- CCScene* HelloWorld::scene()
- {
- CCScene * scene = NULL;
- do
- {
- // 'scene' is an autorelease object
- scene = CCScene::create();
- CC_BREAK_IF(! scene);
- // 'layer' is an autorelease object
- HelloWorld *layer = HelloWorld::create();
- CC_BREAK_IF(! layer);
- // add layer as a child to scene
- scene->addChild(layer);
- } while (0);
- // return the scene
- return scene;
- }
- // on "init" you need to initialize your instance
- bool HelloWorld::init()
- {
- bool bRet = false;
- do
- {
- //////////////////////////////////////////////////////////////////////////
- // super init first
- //////////////////////////////////////////////////////////////////////////
- CC_BREAK_IF(! CCLayer::init());
- //////////////////////////////////////////////////////////////////////////
- // add your codes below...
- //////////////////////////////////////////////////////////////////////////
- // 1. Add a menu item with "X" image, which is clicked to quit the program.
- // Create a "close" menu item with close icon, it's an auto release object.
- CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
- "CloseNormal.png",
- "CloseSelected.png",
- this,
- menu_selector(HelloWorld::menuCloseCallback));
- CC_BREAK_IF(! pCloseItem);
- // Place the menu item bottom-right conner.
- pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
- // Create a menu with the "close" menu item, it's an auto release object.
- CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
- pMenu->setPosition(CCPointZero);
- CC_BREAK_IF(! pMenu);
- // Add the menu to HelloWorld layer as a child layer.
- //this->addChild(pMenu, 1);
- // 2. Add a label shows "Hello World".
- // Create a label and initialize with string "Hello World".
- CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
- CC_BREAK_IF(! pLabel);
- // Get window size and place the label upper.
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- pLabel->setPosition(ccp(size.width / 2, size.height - 50));
- // Add the label to HelloWorld layer as a child layer.
- //this->addChild(pLabel, 1);
- // 3. Add add a splash screen, show the cocos2d splash image.
- CCSprite* pSprite = CCSprite::create("map.jpg");
- CC_BREAK_IF(! pSprite);
- // Place the sprite on the center of the screen
- pSprite->setPosition(ccp(size.width/2, size.height/2));
- // Add the sprite to HelloWorld layer as a child layer.
- this->addChild(pSprite, -1);
- setTouchEnabled(true);
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
- m_nStep = STEP_STARTPOINT;
- m_nStartIndex = -1;
- m_nEndIndex = -1;
- InitMap();
- bRet = true;
- } while (0);
- return bRet;
- }
- void HelloWorld::menuCloseCallback(CCObject* pSender)
- {
- // "close" menu item clicked
- CCDirector::sharedDirector()->end();
- }
- //////////////////////////////////////////////////////////////////////////
- void HelloWorld::InitMap()
- {
- srand((unsigned int)time(NULL));
- for (int i = 0; i < GetRow() * GetCol(); i++)
- {
- int nRandFlag = ((int)(CCRANDOM_0_1() * 10)) % 4 == 0 ? GRID_FLAG_OBSTACLE : GRID_FLAG_DEFAULT;
- ST_GRID* pGrid = new ST_GRID;
- if (!pGrid)
- {
- return ;
- }
- pGrid->gf = (GRID_FLAG)nRandFlag;
- m_vecGrid.push_back(pGrid);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void HelloWorld::FindPath()
- {
- vector<NODE*> vecClose;
- vector<NODE*> vecOpen;
- if (m_nStartIndex == -1 || m_nEndIndex == -1)
- {
- return ;
- }
- m_vecPath.clear();
- // 先添加开始点
- NODE* pNode = new NODE;
- pNode->nIndex = m_nStartIndex;
- vecClose.push_back(pNode);
- int nStep = 0;
- while(true)
- {
- if (nStep++ >= 200)
- {
- break;
- }
- NODE* pNextNode = vecClose[vecClose.size() - 1];
- if (!pNextNode)
- {
- break;
- }
- if (pNextNode->nIndex == m_nEndIndex)
- {
- break;
- }
- for (int i = 0; i < 8; i++)
- {
- int nIndex = GetIndexByDir(pNextNode->nIndex, i);
- if (-1 == nIndex)
- {
- continue;
- }
- if (m_vecGrid[nIndex]->gf == GRID_FLAG_OBSTACLE) // 障碍
- {
- continue;
- }
- if (InTable(nIndex, vecClose) != NULL) // 在close表里
- {
- continue;
- }
- NODE* pNode = InTable(nIndex, vecOpen); // 在open表里
- if (pNode)
- {
- int nNewG = pNextNode->nG + GetGByIndex(pNextNode->nIndex, pNode->nIndex);
- if (pNode->nG > nNewG)
- {
- pNode->nG = nNewG;
- pNode->pParent = pNextNode;
- }
- continue;
- }
- // 新搜索到的格子
- pNode = new NODE;
- pNode->nIndex = nIndex;
- pNode->nG = pNextNode->nG + GetGByIndex(pNextNode->nIndex, pNode->nIndex);
- pNode->pParent = pNextNode;
- vecOpen.push_back(pNode);
- }
- int nMinF = 0xFFFFFF;
- pNextNode = NULL;
- int nNextNodeIndex = 0;
- for (int i = 0; i < (int)vecOpen.size(); i++)
- {
- NODE* pNode = vecOpen[i];
- if (!pNode)
- {
- continue;
- }
- int nH = GetHByIndex(pNode->nIndex);
- int nF = nH + pNode->nG;
- if (nF < nMinF)
- {
- nMinF = nF;
- pNextNode = pNode;
- nNextNodeIndex = i;
- }
- }
- if (nNextNodeIndex >= 0 && nNextNodeIndex < (int)vecOpen.size())
- {
- vecClose.push_back(pNextNode);
- vecOpen.erase(vecOpen.begin() + nNextNodeIndex);
- }
- }
- // 寻路结束,找最优路径
- pNode = vecClose[vecClose.size() - 1];
- while (pNode)
- {
- m_vecPath.push_back(pNode);
- pNode = pNode->pParent;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- int HelloWorld::GetIndexByDir(int nIndex, int nDir)
- {
- if (nIndex < 0 || nIndex >= (int)m_vecGrid.size())
- {
- return -1;
- }
- int nRow = nIndex / GetCol();
- int nCol = nIndex % GetCol();
- switch(nDir)
- {
- case 0: // 上
- nRow += 1;
- break;
- case 1: // 右上
- nRow += 1;
- nCol +=1;
- break;
- case 2: // 右
- nCol += 1;
- break;
- case 3: // 右下
- nRow -= 1;
- nCol += 1;
- break;
- case 4: // 下
- nRow -= 1;
- break;
- case 5: // 左下
- nRow -= 1;
- nCol -= 1;
- break;
- case 6: // 左
- nCol -= 1;
- break;
- case 7: // 左上
- nRow += 1;
- nCol -= 1;
- break;
- default:
- break;
- }
- if (nRow < 0 || nRow >= GetRow()
- || nCol < 0 || nCol >= GetCol())
- {
- return -1;
- }
- return nRow * GetCol() + nCol;
- }
- //////////////////////////////////////////////////////////////////////////
- int HelloWorld::GetGByIndex(int nStartIndex, int nEndIndex)
- {
- int nStartRow = nStartIndex / GetCol();
- int nStartCol = nStartIndex % GetCol();
- int nEndRow = nEndIndex / GetCol();
- int nEndCol = nEndIndex % GetCol();
- if (nStartRow == nEndRow || nStartCol == nEndCol)
- {
- return 10;
- }
- return 14;
- }
- //////////////////////////////////////////////////////////////////////////
- int HelloWorld::GetHByIndex(int nIndex)
- {
- int nRow = nIndex / GetCol();
- int nCol = nIndex % GetCol();
- int nEndRow = m_nEndIndex / GetCol();
- int nEndCol = m_nEndIndex % GetCol();
- return (abs(nEndRow - nRow) + abs(nEndCol - nCol))*10;
- }
- //////////////////////////////////////////////////////////////////////////
- HelloWorld::NODE *HelloWorld::InTable(int nIndex, vector<NODE*> &vecTbl)
- {
- for (int i = 0; i < (int)vecTbl.size(); i++)
- {
- if (nIndex == vecTbl[i]->nIndex)
- {
- return vecTbl[i];
- }
- }
- return NULL;
- }
- //////////////////////////////////////////////////////////////////////////
- int HelloWorld::GetRow()
- {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- return size.height / GRID_SIDELEN;
- }
- //////////////////////////////////////////////////////////////////////////
- int HelloWorld::GetCol()
- {
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- return size.width / GRID_SIDELEN;
- }
- //////////////////////////////////////////////////////////////////////////
- int HelloWorld::GetIndexByPoint(CCPoint pt)
- {
- pt.x = pt.x > (int)pt.x ? pt.x + 1 : pt.x;
- pt.y = pt.y > (int)pt.y ? pt.y + 1 : pt.y;
- return (int)pt.y / GRID_SIDELEN * GetCol() + (int)pt.x / GRID_SIDELEN;
- }
- //////////////////////////////////////////////////////////////////////////
- void HelloWorld::draw()
- {
- // 画背景表格
- CCSize size = CCDirector::sharedDirector()->getWinSize();
- for (int i = 0; i < GetRow(); i++)
- {
- ccDrawLine(ccp(0, i * GRID_SIDELEN), ccp(size.width, i * GRID_SIDELEN));
- }
- for (int i = 0; i < GetCol(); i++)
- {
- ccDrawLine(ccp(i * GRID_SIDELEN, 0), ccp(i * GRID_SIDELEN, size.height));
- }
- // 画特殊格子颜色
- // 寻路得到的路径
- for (int i = 0; i < (int)m_vecPath.size(); i++)
- {
- CCPoint ptObstacleLT;
- CCPoint ptObstacleRD;
- GetRectPointByIndex(m_vecPath[i]->nIndex, ptObstacleLT, ptObstacleRD);
- ccColor4F clrObstacle = {0, 1, 1, 1};
- ccDrawSolidRect(ptObstacleLT, ptObstacleRD, clrObstacle);
- }
- // 开始点
- CCPoint ptStartLT;
- CCPoint ptStartRD;
- GetRectPointByIndex(m_nStartIndex, ptStartLT, ptStartRD);
- ccColor4F clrStart = {1, 0, 0, 1};
- ccDrawSolidRect(ptStartLT, ptStartRD, clrStart);
- // 结束点
- CCPoint ptEndLT;
- CCPoint ptEndRD;
- GetRectPointByIndex(m_nEndIndex, ptEndLT, ptEndRD);
- ccColor4F clrEnd = {0, 1, 0, 1};
- ccDrawSolidRect(ptEndLT, ptEndRD, clrEnd);
- // 障碍
- for (int i = 0; i < (int)m_vecGrid.size(); i++)
- {
- if (m_vecGrid[i]->gf == GRID_FLAG_OBSTACLE)
- {
- CCPoint ptObstacleLT;
- CCPoint ptObstacleRD;
- GetRectPointByIndex(i, ptObstacleLT, ptObstacleRD);
- ccColor4F clrObstacle = {0, 0, 1, 1};
- ccDrawSolidRect(ptObstacleLT, ptObstacleRD, clrObstacle);
- }
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void HelloWorld::GetRectPointByIndex(int nIndex, CCPoint &ptLT, CCPoint &ptRD)
- {
- ptLT.x = nIndex % GetCol() * GRID_SIDELEN;
- ptLT.y = nIndex / GetCol() * GRID_SIDELEN;
- ptRD.x = ptLT.x + GRID_SIDELEN;
- ptRD.y = ptLT.y + GRID_SIDELEN;
- }
- //////////////////////////////////////////////////////////////////////////
- bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
- {
- if (!pTouch)
- {
- return false;
- }
- int nIndex = GetIndexByPoint(pTouch->getLocation());
- if (m_vecGrid[nIndex]->gf == GRID_FLAG_OBSTACLE)
- {
- return false;
- }
- if (STEP_STARTPOINT == m_nStep)
- {
- m_nStartIndex = nIndex;
- m_nStep = STEP_ENDPOINT;
- }
- else if (STEP_ENDPOINT == m_nStep)
- {
- m_nEndIndex = nIndex;
- m_nStep = STEP_STARTPOINT;
- FindPath();
- }
- return true;
- }
哎,想来想去,还是直接上源码比较直截了当。。。。。寻路效果如下,红色是起点,绿色是终点,蓝色是障碍物,浅蓝色是最终寻路路径:
cocos2d-x的A*寻路的更多相关文章
- cocos2d-x学习日志(13) --A星寻路算法demo
你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢?如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! A星算法简介: A*搜寻算法俗称A星 ...
- 如何在Cocos2D游戏中实现A*寻路算法(六)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 如何在Cocos2D游戏中实现A*寻路算法(一)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 如何在Cocos2D游戏中实现A*寻路算法(八)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 如何在Cocos2D游戏中实现A*寻路算法(七)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 如何在Cocos2D游戏中实现A*寻路算法(五)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 如何在Cocos2D游戏中实现A*寻路算法(四)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 如何在Cocos2D游戏中实现A*寻路算法(三)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 如何在Cocos2D游戏中实现A*寻路算法(二)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- A星寻路算法介绍
你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢? 如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! 在网上已经有很多篇关于A星寻路算法 ...
随机推荐
- JavaSE_ Java基础 总目录(1~6)
JavaSE学习总结第01天_Java概述01.01 计算机概述01.02 计算机硬件和软件概述01.03 软件开发和计算机语言概述01.04 人机交互01.05 键盘功能键和快捷键01.06 如何打 ...
- jbpmAPI-8
8.1. Process Instance State jBPM允许某些信息的持久性存储.本章描述了这些不同类型的持久性,以及如何配置它们.存储的信息的一个例子是运行时状态的过程.存储过程运行时状态是 ...
- Protel99se教程八:protel99se原理图设计的高级应用
在我们PCB资源网的前边的protel99se教程当中,我们给大家讲解了如何绘制一个简单的原理图,以及如何将SCH原理图转为PCB,再有就是创建SCH元件,以及如何建立protel99se封库,有了上 ...
- PCB的整个加工流程
1 MI:制作生产流程卡,指导产线如何去生产出所需要的pcb.2 内层:PCB,除了最便宜的单层板,简单的双层板,有时候需要使用4层 6层 8层,以实现复杂的连 接关系和高密度,再就是减少干扰或者降低 ...
- Android PNG渐变背景图片失真问题 getWindow().setFormat(PixelFormat.RGBA_8888);
最近一个困扰很久的问题,渐变效果的png图片,设置为控件图片或background时,在eclipse上看着没有什么问题,但是在设备上运行时,可以看到明显的一圈圈的轮廓线,图片严重失真.在网上goog ...
- json数据与字符串的相互转化
json转成string[需要引用json2.js文件]: var arr=[{id:'id',name:'Spring'},{id:'id2',name:'Jane'}]; var str=JSON ...
- 获取中央气象台API 完整城市列表简单方式
activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android& ...
- SQL实现递归及存储过程中 In() 参数传递解决方案
1.SQL递归 在SQL Server中,我们可以利用表表达式来实现递归算法,一般用于阻止机构的加载及相关性处理. -->实现: 假设OrganiseUnit(组织机构表)中主要的三个字段为Or ...
- 2014-CVTE网测部分软件技术测试题及答案
1.叉树的先序遍历序列和后序遍历序列正好相反,则该二叉树满足的条件是(D) A.空或只有一个结点 B.高度等于其结点数 C.该二叉树是完全二叉树 D.所有结点无右孩子 应该是二叉树的每个结点都只有一个 ...
- for语句的嵌套(示例及练习)
for(初始条件:循环条件:状态改变) {for(初始条件:循环条件:状态改变) { 循环体 }} 一般,用来解决循环的方法:穷举法.迭代法. 示例一:阶乘的和 示例二: 练习一:兔 ...