如果你还不熟悉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、初始化地图

声明结构表示格子掩码等一些信息,我们的例子中,只需掩码,所以结构如下:

  1. struct ST_GRID
  2. {
  3. ST_GRID() { gf = GRID_FLAG_DEFAULT; }
  4. GRID_FLAG gf;
  5. };
  6. typedef vector<ST_GRID*> VEC_GRID;
  7. VEC_GRID m_vecGrid; // 保存地图产生的所有格子

初始化地图的格子掩码,如下:

  1. void HelloWorld::InitMap()
  2. {
  3. // 初始化格子掩码
  4.  srand((unsigned int)time(NULL));
  5. for (int i = 0; i < GetRow() * GetCol(); i++)
  6. {
  7. int nRandFlag = ((int)(CCRANDOM_0_1() * 10)) % 4 == 0 ? GRID_FLAG_OBSTACLE : GRID_FLAG_DEFAULT; // 十分之四的概率产生障碍
  8. ST_GRID* pGrid = new ST_GRID;
  9. if (!pGrid)
  10. {
  11. return ;
  12. }
  13. pGrid->gf = (GRID_FLAG)nRandFlag;
  14. m_vecGrid.push_back(pGrid);
  15. }
  16. }

3、寻路

定义一个结构,用于寻路过程中,记录每个格子的信息

  1. struct NODE
  2. {
  3. NODE() {nIndex = 0; nG = 0; pParent = NULL;}
  4. int nIndex;
  5. int nG;
  6. NODE* pParent;
  7. };
  8. vector<NODE*> m_vecPath; // 寻路的路径

下面开始寻路

  1. void HelloWorld::FindPath()
  2. {
  3. vector<NODE*> vecClose; // close表
  4. vector<NODE*> vecOpen; // open表
  5. if (m_nStartIndex == -1 || m_nEndIndex == -1)
  6. {
  7. return ;
  8. }
  9. m_vecPath.clear(); // 这里,我们并没有delete,但却不会内存泄漏,因为cocos2d-x使用了跟java一样的技术 -- 内存回收机制,自动处理垃圾
  10.  
  11. // 先添加开始点
  12. NODE* pNode = new NODE;
  13. pNode->nIndex = m_nStartIndex;
  14. vecClose.push_back(pNode);
  15.  
  16. int nStep = 0;
  17. while(true)
  18. {
  19. if (nStep++ >= 200) // 最多寻200格
  20. {
  21. break;
  22. }
  23. NODE* pNextNode = vecClose[vecClose.size() - 1]; // 取下一个路径
  24. if (!pNextNode)
  25. {
  26. break;
  27. }
  28. if (pNextNode->nIndex == m_nEndIndex) // 找到终点,就不再找了
  29. {
  30. break;
  31. }
  32.  
  33. for (int i = 0; i < 8; i++)
  34. {
  35. int nIndex = GetIndexByDir(pNextNode->nIndex, i); // 根据方向取索引
  36. if (-1 == nIndex)
  37. {
  38. continue;
  39. }
  40. if (m_vecGrid[nIndex]->gf == GRID_FLAG_OBSTACLE) // 障碍
  41. {
  42. continue;
  43. }
  44. if (InTable(nIndex, vecClose) != NULL) // 在close表里
  45. {
  46. continue;
  47. }
  48.  
  49. NODE* pNode = InTable(nIndex, vecOpen); // 在open表里,比较G值,取G值更小的为新路径
  50. if (pNode)
  51. {
  52. int nNewG = pNextNode->nG + GetGByIndex(pNextNode->nIndex, pNode->nIndex);
  53. if (pNode->nG > nNewG)
  54. {
  55. pNode->nG = nNewG;
  56. pNode->pParent = pNextNode; // 改变节点的父节点
  57. }
  58. continue;
  59. }
  60.  
  61. // 新搜索到的格子,添加到开放列表
  62. pNode = new NODE;
  63. pNode->nIndex = nIndex;
  64. pNode->nG = pNextNode->nG + GetGByIndex(pNextNode->nIndex, pNode->nIndex);
  65. pNode->pParent = pNextNode;
  66. vecOpen.push_back(pNode);
  67. }
  68.  
  69. // 找下一个路径,open表里F值最小的就是了
  70. int nMinF = 0xFFFFFF;
  71. pNextNode = NULL;
  72. int nNextNodeIndex = 0;
  73. for (int i = 0; i < (int)vecOpen.size(); i++)
  74. {
  75. NODE* pNode = vecOpen[i];
  76. if (!pNode)
  77. {
  78. continue;
  79. }
  80. int nH = GetHByIndex(pNode->nIndex); // 计算该点与终点的H值,即路径长度
  81. int nF = nH + pNode->nG; // F = H + G
  82. if (nF < nMinF)
  83. {
  84. nMinF = nF;
  85. pNextNode = pNode;
  86. nNextNodeIndex = i;
  87. }
  88. }
  89. // 找到F值最小的,放入close表,并从open表里删除
  90.  if (nNextNodeIndex >= 0 && nNextNodeIndex < (int)vecOpen.size())
  91. {
  92. vecClose.push_back(pNextNode);
  93. vecOpen.erase(vecOpen.begin() + nNextNodeIndex);
  94. }
  95. }
  96.  
  97. // 寻路结束,找最优路径
  98. pNode = vecClose[vecClose.size() - 1];
  99. while (pNode)
  100. {
  101. m_vecPath.push_back(pNode);
  102. pNode = pNode->pParent;
  103. }
  104. }

4、展示到界面上

  1. void HelloWorld::draw()
  2. {
  3. // 画背景表格
  4. CCSize size = CCDirector::sharedDirector()->getWinSize();
  5. for (int i = 0; i < GetRow(); i++)
  6. {
  7. ccDrawLine(ccp(0, i * GRID_SIDELEN), ccp(size.width, i * GRID_SIDELEN));
  8. }
  9. for (int i = 0; i < GetCol(); i++)
  10. {
  11. ccDrawLine(ccp(i * GRID_SIDELEN, 0), ccp(i * GRID_SIDELEN, size.height));
  12. }
  13.  
  14. // 画特殊格子颜色
  15. // 寻路得到的路径
  16. for (int i = 0; i < (int)m_vecPath.size(); i++)
  17. {
  18. CCPoint ptObstacleLT;
  19. CCPoint ptObstacleRD;
  20. GetRectPointByIndex(m_vecPath[i]->nIndex, ptObstacleLT, ptObstacleRD);
  21. ccColor4F clrObstacle = {0, 1, 1, 0};
  22. ccDrawSolidRect(ptObstacleLT, ptObstacleRD, clrObstacle);
  23. }
  24.  
  25. // 开始点
  26. CCPoint ptStartLT;
  27. CCPoint ptStartRD;
  28. GetRectPointByIndex(m_nStartIndex, ptStartLT, ptStartRD);
  29. ccColor4F clrStart = {1, 0, 0, 1};
  30. ccDrawSolidRect(ptStartLT, ptStartRD, clrStart);
  31.  
  32. // 结束点
  33. CCPoint ptEndLT;
  34. CCPoint ptEndRD;
  35. GetRectPointByIndex(m_nEndIndex, ptEndLT, ptEndRD);
  36. ccColor4F clrEnd = {0, 1, 0, 1};
  37. ccDrawSolidRect(ptEndLT, ptEndRD, clrEnd);
  38.  
  39. // 障碍
  40. for (int i = 0; i < (int)m_vecGrid.size(); i++)
  41. {
  42. if (m_vecGrid[i]->gf == GRID_FLAG_OBSTACLE)
  43. {
  44. CCPoint ptObstacleLT;
  45. CCPoint ptObstacleRD;
  46. GetRectPointByIndex(i, ptObstacleLT, ptObstacleRD);
  47. ccColor4F clrObstacle = {0, 0, 1, 1};
  48. ccDrawSolidRect(ptObstacleLT, ptObstacleRD, clrObstacle);
  49. }
  50. }
  51. }

这里,我只介绍几个比较重要的函数,其他的就不赘述了,资源已上传到CSDN,但还没显示出来,等显示出来了,再把链接发到此处,有疑问的童鞋留言哈

。。。。。。。。

啊,我还是直接上源码吧

  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3.  
  4. #include "cocos2d.h"
  5. #include "Box2D/Box2D.h"
  6. #include "SimpleAudioEngine.h"
  7. USING_NS_CC;
  8.  
  9. enum STEP
  10. {
  11. STEP_DEFAULT = 0,
  12. STEP_STARTPOINT = 1,
  13. STEP_ENDPOINT = 2,
  14. };
  15.  
  16. enum GRID_FLAG
  17. {
  18. GRID_FLAG_DEFAULT = 0, // 默认可通过
  19. GRID_FLAG_OBSTACLE = 1, // 障碍
  20. };
  21. const int GRID_SIDELEN = 20; // 不能为0
  22. //////////////////////////////////////////////////////////////////////////
  23. class HelloWorld : public cocos2d::CCLayer
  24. {
  25. public:
  26. // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
  27. virtual bool init();
  28.  
  29. // there's no 'id' in cpp, so we recommand to return the exactly class pointer
  30. static cocos2d::CCScene* scene();
  31.  
  32. // a selector callback
  33. void menuCloseCallback(CCObject* pSender);
  34.  
  35. // implement the "static node()" method manually
  36. CREATE_FUNC(HelloWorld);
  37.  
  38. public:
  39. virtual void draw();
  40. virtual bool ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent);
  41.  
  42. private:
  43. void InitMap();
  44.  
  45. private:
  46. int GetRow();
  47. int GetCol();
  48. int GetIndexByPoint(CCPoint pt);
  49. void GetRectPointByIndex(int nIndex, CCPoint &ptLT, CCPoint &ptRD);
  50.  
  51. private:
  52. int m_nStartIndex;
  53. int m_nEndIndex;
  54.  
  55. struct ST_GRID
  56. {
  57. ST_GRID() { gf = GRID_FLAG_DEFAULT; }
  58. GRID_FLAG gf;
  59. };
  60. typedef vector<ST_GRID*> VEC_GRID;
  61. VEC_GRID m_vecGrid;
  62.  
  63. struct NODE
  64. {
  65. NODE() {nIndex = 0; nG = 0; pParent = NULL;}
  66. int nIndex;
  67. int nG;
  68. NODE* pParent;
  69. };
  70. vector<NODE*> m_vecPath; // 寻路的路径
  71.  
  72. public:
  73. void FindPath();
  74.  
  75. private:
  76. int GetIndexByDir(int nIndex, int nDir);
  77. int GetGByIndex(int nStartIndex, int nEndIndex);
  78. int GetHByIndex(int nIndex);
  79. NODE *InTable(int nIndex, vector<NODE*> &vecTbl);
  80.  
  81. private:
  82. int m_nStep;
  83. };
  84.  
  85. #endif // __HELLOWORLD_SCENE_H__
  1. #include "HelloWorldScene.h"
  2.  
  3. using namespace cocos2d;
  4.  
  5. CCScene* HelloWorld::scene()
  6. {
  7. CCScene * scene = NULL;
  8. do
  9. {
  10. // 'scene' is an autorelease object
  11. scene = CCScene::create();
  12. CC_BREAK_IF(! scene);
  13.  
  14. // 'layer' is an autorelease object
  15. HelloWorld *layer = HelloWorld::create();
  16. CC_BREAK_IF(! layer);
  17.  
  18. // add layer as a child to scene
  19. scene->addChild(layer);
  20. } while (0);
  21.  
  22. // return the scene
  23. return scene;
  24. }
  25.  
  26. // on "init" you need to initialize your instance
  27. bool HelloWorld::init()
  28. {
  29. bool bRet = false;
  30. do
  31. {
  32. //////////////////////////////////////////////////////////////////////////
  33. // super init first
  34. //////////////////////////////////////////////////////////////////////////
  35.  
  36. CC_BREAK_IF(! CCLayer::init());
  37.  
  38. //////////////////////////////////////////////////////////////////////////
  39. // add your codes below...
  40. //////////////////////////////////////////////////////////////////////////
  41.  
  42. // 1. Add a menu item with "X" image, which is clicked to quit the program.
  43.  
  44. // Create a "close" menu item with close icon, it's an auto release object.
  45. CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
  46. "CloseNormal.png",
  47. "CloseSelected.png",
  48. this,
  49. menu_selector(HelloWorld::menuCloseCallback));
  50. CC_BREAK_IF(! pCloseItem);
  51.  
  52. // Place the menu item bottom-right conner.
  53. pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
  54.  
  55. // Create a menu with the "close" menu item, it's an auto release object.
  56. CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
  57. pMenu->setPosition(CCPointZero);
  58. CC_BREAK_IF(! pMenu);
  59.  
  60. // Add the menu to HelloWorld layer as a child layer.
  61. //this->addChild(pMenu, 1);
  62.  
  63. // 2. Add a label shows "Hello World".
  64.  
  65. // Create a label and initialize with string "Hello World".
  66. CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
  67. CC_BREAK_IF(! pLabel);
  68.  
  69. // Get window size and place the label upper.
  70. CCSize size = CCDirector::sharedDirector()->getWinSize();
  71. pLabel->setPosition(ccp(size.width / 2, size.height - 50));
  72.  
  73. // Add the label to HelloWorld layer as a child layer.
  74. //this->addChild(pLabel, 1);
  75.  
  76. // 3. Add add a splash screen, show the cocos2d splash image.
  77. CCSprite* pSprite = CCSprite::create("map.jpg");
  78. CC_BREAK_IF(! pSprite);
  79.  
  80. // Place the sprite on the center of the screen
  81. pSprite->setPosition(ccp(size.width/2, size.height/2));
  82.  
  83. // Add the sprite to HelloWorld layer as a child layer.
  84. this->addChild(pSprite, -1);
  85.  
  86. setTouchEnabled(true);
  87. CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
  88.  
  89. m_nStep = STEP_STARTPOINT;
  90. m_nStartIndex = -1;
  91. m_nEndIndex = -1;
  92. InitMap();
  93.  
  94. bRet = true;
  95. } while (0);
  96.  
  97. return bRet;
  98. }
  99.  
  100. void HelloWorld::menuCloseCallback(CCObject* pSender)
  101. {
  102. // "close" menu item clicked
  103. CCDirector::sharedDirector()->end();
  104. }
  105.  
  106. //////////////////////////////////////////////////////////////////////////
  107. void HelloWorld::InitMap()
  108. {
  109. srand((unsigned int)time(NULL));
  110. for (int i = 0; i < GetRow() * GetCol(); i++)
  111. {
  112. int nRandFlag = ((int)(CCRANDOM_0_1() * 10)) % 4 == 0 ? GRID_FLAG_OBSTACLE : GRID_FLAG_DEFAULT;
  113. ST_GRID* pGrid = new ST_GRID;
  114. if (!pGrid)
  115. {
  116. return ;
  117. }
  118. pGrid->gf = (GRID_FLAG)nRandFlag;
  119. m_vecGrid.push_back(pGrid);
  120. }
  121. }
  122.  
  123. //////////////////////////////////////////////////////////////////////////
  124. void HelloWorld::FindPath()
  125. {
  126. vector<NODE*> vecClose;
  127. vector<NODE*> vecOpen;
  128. if (m_nStartIndex == -1 || m_nEndIndex == -1)
  129. {
  130. return ;
  131. }
  132. m_vecPath.clear();
  133.  
  134. // 先添加开始点
  135. NODE* pNode = new NODE;
  136. pNode->nIndex = m_nStartIndex;
  137. vecClose.push_back(pNode);
  138.  
  139. int nStep = 0;
  140. while(true)
  141. {
  142. if (nStep++ >= 200)
  143. {
  144. break;
  145. }
  146. NODE* pNextNode = vecClose[vecClose.size() - 1];
  147. if (!pNextNode)
  148. {
  149. break;
  150. }
  151. if (pNextNode->nIndex == m_nEndIndex)
  152. {
  153. break;
  154. }
  155.  
  156. for (int i = 0; i < 8; i++)
  157. {
  158. int nIndex = GetIndexByDir(pNextNode->nIndex, i);
  159. if (-1 == nIndex)
  160. {
  161. continue;
  162. }
  163. if (m_vecGrid[nIndex]->gf == GRID_FLAG_OBSTACLE) // 障碍
  164. {
  165. continue;
  166. }
  167. if (InTable(nIndex, vecClose) != NULL) // 在close表里
  168. {
  169. continue;
  170. }
  171.  
  172. NODE* pNode = InTable(nIndex, vecOpen); // 在open表里
  173. if (pNode)
  174. {
  175. int nNewG = pNextNode->nG + GetGByIndex(pNextNode->nIndex, pNode->nIndex);
  176. if (pNode->nG > nNewG)
  177. {
  178. pNode->nG = nNewG;
  179. pNode->pParent = pNextNode;
  180. }
  181. continue;
  182. }
  183.  
  184. // 新搜索到的格子
  185. pNode = new NODE;
  186. pNode->nIndex = nIndex;
  187. pNode->nG = pNextNode->nG + GetGByIndex(pNextNode->nIndex, pNode->nIndex);
  188. pNode->pParent = pNextNode;
  189. vecOpen.push_back(pNode);
  190. }
  191.  
  192. int nMinF = 0xFFFFFF;
  193. pNextNode = NULL;
  194. int nNextNodeIndex = 0;
  195. for (int i = 0; i < (int)vecOpen.size(); i++)
  196. {
  197. NODE* pNode = vecOpen[i];
  198. if (!pNode)
  199. {
  200. continue;
  201. }
  202. int nH = GetHByIndex(pNode->nIndex);
  203. int nF = nH + pNode->nG;
  204. if (nF < nMinF)
  205. {
  206. nMinF = nF;
  207. pNextNode = pNode;
  208. nNextNodeIndex = i;
  209. }
  210. }
  211. if (nNextNodeIndex >= 0 && nNextNodeIndex < (int)vecOpen.size())
  212. {
  213. vecClose.push_back(pNextNode);
  214. vecOpen.erase(vecOpen.begin() + nNextNodeIndex);
  215. }
  216. }
  217.  
  218. // 寻路结束,找最优路径
  219. pNode = vecClose[vecClose.size() - 1];
  220. while (pNode)
  221. {
  222. m_vecPath.push_back(pNode);
  223. pNode = pNode->pParent;
  224. }
  225. }
  226.  
  227. //////////////////////////////////////////////////////////////////////////
  228. int HelloWorld::GetIndexByDir(int nIndex, int nDir)
  229. {
  230. if (nIndex < 0 || nIndex >= (int)m_vecGrid.size())
  231. {
  232. return -1;
  233. }
  234.  
  235. int nRow = nIndex / GetCol();
  236. int nCol = nIndex % GetCol();
  237.  
  238. switch(nDir)
  239. {
  240. case 0: // 上
  241. nRow += 1;
  242. break;
  243. case 1: // 右上
  244. nRow += 1;
  245. nCol +=1;
  246. break;
  247. case 2: // 右
  248. nCol += 1;
  249. break;
  250. case 3: // 右下
  251. nRow -= 1;
  252. nCol += 1;
  253. break;
  254. case 4: // 下
  255. nRow -= 1;
  256. break;
  257. case 5: // 左下
  258. nRow -= 1;
  259. nCol -= 1;
  260. break;
  261. case 6: // 左
  262. nCol -= 1;
  263. break;
  264. case 7: // 左上
  265. nRow += 1;
  266. nCol -= 1;
  267. break;
  268. default:
  269. break;
  270. }
  271. if (nRow < 0 || nRow >= GetRow()
  272. || nCol < 0 || nCol >= GetCol())
  273. {
  274. return -1;
  275. }
  276. return nRow * GetCol() + nCol;
  277. }
  278.  
  279. //////////////////////////////////////////////////////////////////////////
  280. int HelloWorld::GetGByIndex(int nStartIndex, int nEndIndex)
  281. {
  282. int nStartRow = nStartIndex / GetCol();
  283. int nStartCol = nStartIndex % GetCol();
  284.  
  285. int nEndRow = nEndIndex / GetCol();
  286. int nEndCol = nEndIndex % GetCol();
  287.  
  288. if (nStartRow == nEndRow || nStartCol == nEndCol)
  289. {
  290. return 10;
  291. }
  292. return 14;
  293. }
  294.  
  295. //////////////////////////////////////////////////////////////////////////
  296. int HelloWorld::GetHByIndex(int nIndex)
  297. {
  298. int nRow = nIndex / GetCol();
  299. int nCol = nIndex % GetCol();
  300.  
  301. int nEndRow = m_nEndIndex / GetCol();
  302. int nEndCol = m_nEndIndex % GetCol();
  303.  
  304. return (abs(nEndRow - nRow) + abs(nEndCol - nCol))*10;
  305. }
  306.  
  307. //////////////////////////////////////////////////////////////////////////
  308. HelloWorld::NODE *HelloWorld::InTable(int nIndex, vector<NODE*> &vecTbl)
  309. {
  310. for (int i = 0; i < (int)vecTbl.size(); i++)
  311. {
  312. if (nIndex == vecTbl[i]->nIndex)
  313. {
  314. return vecTbl[i];
  315. }
  316. }
  317. return NULL;
  318. }
  319.  
  320. //////////////////////////////////////////////////////////////////////////
  321. int HelloWorld::GetRow()
  322. {
  323. CCSize size = CCDirector::sharedDirector()->getWinSize();
  324. return size.height / GRID_SIDELEN;
  325. }
  326.  
  327. //////////////////////////////////////////////////////////////////////////
  328. int HelloWorld::GetCol()
  329. {
  330. CCSize size = CCDirector::sharedDirector()->getWinSize();
  331. return size.width / GRID_SIDELEN;
  332. }
  333.  
  334. //////////////////////////////////////////////////////////////////////////
  335. int HelloWorld::GetIndexByPoint(CCPoint pt)
  336. {
  337. pt.x = pt.x > (int)pt.x ? pt.x + 1 : pt.x;
  338. pt.y = pt.y > (int)pt.y ? pt.y + 1 : pt.y;
  339. return (int)pt.y / GRID_SIDELEN * GetCol() + (int)pt.x / GRID_SIDELEN;
  340. }
  341.  
  342. //////////////////////////////////////////////////////////////////////////
  343. void HelloWorld::draw()
  344. {
  345. // 画背景表格
  346. CCSize size = CCDirector::sharedDirector()->getWinSize();
  347. for (int i = 0; i < GetRow(); i++)
  348. {
  349. ccDrawLine(ccp(0, i * GRID_SIDELEN), ccp(size.width, i * GRID_SIDELEN));
  350. }
  351. for (int i = 0; i < GetCol(); i++)
  352. {
  353. ccDrawLine(ccp(i * GRID_SIDELEN, 0), ccp(i * GRID_SIDELEN, size.height));
  354. }
  355.  
  356. // 画特殊格子颜色
  357. // 寻路得到的路径
  358. for (int i = 0; i < (int)m_vecPath.size(); i++)
  359. {
  360. CCPoint ptObstacleLT;
  361. CCPoint ptObstacleRD;
  362. GetRectPointByIndex(m_vecPath[i]->nIndex, ptObstacleLT, ptObstacleRD);
  363. ccColor4F clrObstacle = {0, 1, 1, 1};
  364. ccDrawSolidRect(ptObstacleLT, ptObstacleRD, clrObstacle);
  365. }
  366.  
  367. // 开始点
  368. CCPoint ptStartLT;
  369. CCPoint ptStartRD;
  370. GetRectPointByIndex(m_nStartIndex, ptStartLT, ptStartRD);
  371. ccColor4F clrStart = {1, 0, 0, 1};
  372. ccDrawSolidRect(ptStartLT, ptStartRD, clrStart);
  373.  
  374. // 结束点
  375. CCPoint ptEndLT;
  376. CCPoint ptEndRD;
  377. GetRectPointByIndex(m_nEndIndex, ptEndLT, ptEndRD);
  378. ccColor4F clrEnd = {0, 1, 0, 1};
  379. ccDrawSolidRect(ptEndLT, ptEndRD, clrEnd);
  380.  
  381. // 障碍
  382. for (int i = 0; i < (int)m_vecGrid.size(); i++)
  383. {
  384. if (m_vecGrid[i]->gf == GRID_FLAG_OBSTACLE)
  385. {
  386. CCPoint ptObstacleLT;
  387. CCPoint ptObstacleRD;
  388. GetRectPointByIndex(i, ptObstacleLT, ptObstacleRD);
  389. ccColor4F clrObstacle = {0, 0, 1, 1};
  390. ccDrawSolidRect(ptObstacleLT, ptObstacleRD, clrObstacle);
  391. }
  392. }
  393. }
  394.  
  395. //////////////////////////////////////////////////////////////////////////
  396. void HelloWorld::GetRectPointByIndex(int nIndex, CCPoint &ptLT, CCPoint &ptRD)
  397. {
  398. ptLT.x = nIndex % GetCol() * GRID_SIDELEN;
  399. ptLT.y = nIndex / GetCol() * GRID_SIDELEN;
  400. ptRD.x = ptLT.x + GRID_SIDELEN;
  401. ptRD.y = ptLT.y + GRID_SIDELEN;
  402. }
  403.  
  404. //////////////////////////////////////////////////////////////////////////
  405. bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
  406. {
  407. if (!pTouch)
  408. {
  409. return false;
  410. }
  411.  
  412. int nIndex = GetIndexByPoint(pTouch->getLocation());
  413. if (m_vecGrid[nIndex]->gf == GRID_FLAG_OBSTACLE)
  414. {
  415. return false;
  416. }
  417.  
  418. if (STEP_STARTPOINT == m_nStep)
  419. {
  420. m_nStartIndex = nIndex;
  421. m_nStep = STEP_ENDPOINT;
  422. }
  423. else if (STEP_ENDPOINT == m_nStep)
  424. {
  425. m_nEndIndex = nIndex;
  426. m_nStep = STEP_STARTPOINT;
  427. FindPath();
  428. }
  429. return true;
  430. }

哎,想来想去,还是直接上源码比较直截了当。。。。。寻路效果如下,红色是起点,绿色是终点,蓝色是障碍物,浅蓝色是最终寻路路径:

cocos2d-x的A*寻路的更多相关文章

  1. cocos2d-x学习日志(13) --A星寻路算法demo

    你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢?如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! A星算法简介: A*搜寻算法俗称A星 ...

  2. 如何在Cocos2D游戏中实现A*寻路算法(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  3. 如何在Cocos2D游戏中实现A*寻路算法(一)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  4. 如何在Cocos2D游戏中实现A*寻路算法(八)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  5. 如何在Cocos2D游戏中实现A*寻路算法(七)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  6. 如何在Cocos2D游戏中实现A*寻路算法(五)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  7. 如何在Cocos2D游戏中实现A*寻路算法(四)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  8. 如何在Cocos2D游戏中实现A*寻路算法(三)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  9. 如何在Cocos2D游戏中实现A*寻路算法(二)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  10. A星寻路算法介绍

    你是否在做一款游戏的时候想创造一些怪兽或者游戏主角,让它们移动到特定的位置,避开墙壁和障碍物呢? 如果是的话,请看这篇教程,我们会展示如何使用A星寻路算法来实现它! 在网上已经有很多篇关于A星寻路算法 ...

随机推荐

  1. JavaSE_ Java基础 总目录(1~6)

    JavaSE学习总结第01天_Java概述01.01 计算机概述01.02 计算机硬件和软件概述01.03 软件开发和计算机语言概述01.04 人机交互01.05 键盘功能键和快捷键01.06 如何打 ...

  2. jbpmAPI-8

    8.1. Process Instance State jBPM允许某些信息的持久性存储.本章描述了这些不同类型的持久性,以及如何配置它们.存储的信息的一个例子是运行时状态的过程.存储过程运行时状态是 ...

  3. Protel99se教程八:protel99se原理图设计的高级应用

    在我们PCB资源网的前边的protel99se教程当中,我们给大家讲解了如何绘制一个简单的原理图,以及如何将SCH原理图转为PCB,再有就是创建SCH元件,以及如何建立protel99se封库,有了上 ...

  4. PCB的整个加工流程

    1 MI:制作生产流程卡,指导产线如何去生产出所需要的pcb.2 内层:PCB,除了最便宜的单层板,简单的双层板,有时候需要使用4层 6层 8层,以实现复杂的连 接关系和高密度,再就是减少干扰或者降低 ...

  5. Android PNG渐变背景图片失真问题 getWindow().setFormat(PixelFormat.RGBA_8888);

    最近一个困扰很久的问题,渐变效果的png图片,设置为控件图片或background时,在eclipse上看着没有什么问题,但是在设备上运行时,可以看到明显的一圈圈的轮廓线,图片严重失真.在网上goog ...

  6. json数据与字符串的相互转化

    json转成string[需要引用json2.js文件]: var arr=[{id:'id',name:'Spring'},{id:'id2',name:'Jane'}]; var str=JSON ...

  7. 获取中央气象台API 完整城市列表简单方式

    activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android& ...

  8. SQL实现递归及存储过程中 In() 参数传递解决方案

    1.SQL递归 在SQL Server中,我们可以利用表表达式来实现递归算法,一般用于阻止机构的加载及相关性处理. -->实现: 假设OrganiseUnit(组织机构表)中主要的三个字段为Or ...

  9. 2014-CVTE网测部分软件技术测试题及答案

    1.叉树的先序遍历序列和后序遍历序列正好相反,则该二叉树满足的条件是(D) A.空或只有一个结点 B.高度等于其结点数 C.该二叉树是完全二叉树 D.所有结点无右孩子 应该是二叉树的每个结点都只有一个 ...

  10. for语句的嵌套(示例及练习)

    for(初始条件:循环条件:状态改变) {for(初始条件:循环条件:状态改变) {     循环体      }} 一般,用来解决循环的方法:穷举法.迭代法. 示例一:阶乘的和 示例二: 练习一:兔 ...