cocos2dx A*算法

前端之家收集整理的这篇文章主要介绍了cocos2dx A*算法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件和源文件复制到项目中就能用了! have fun

使用cocos2dx 3.2 原理都一样

淡蓝色的点是地图

深蓝色的点是障碍物

绿色的点是路径

暗绿色的点是搜寻过的点

红色的点是按路径行走的点



dijkstra算法 会发现路径最短,但寻找过的路径比较多(计算速度慢)

最佳优先搜索算法会发现寻找过的路径少了(计算速度提高了),但走了许多弯路

A星算法 结合了上面2种算法 即寻找到了最短路径,搜寻过的路径也比较少

[cpp] @L_403_0@ copy
  1. @H_403_39@#ifndef__HELLOWORLD_SCENE_H__@H_502_41@@H_502_41@@H_502_41@
  2. @H_403_39@#define__HELLOWORLD_SCENE_H__@H_502_41@@H_502_41@@H_502_41@
  3. @H_403_39@@H_502_41@
  4. @H_403_39@#include"cocos2d.h"@H_502_41@@H_502_41@@H_502_41@
  5. @H_403_39@#include"vector"@H_502_41@@H_502_41@@H_502_41@
  6. @H_403_39@using@H_502_41@@H_502_41@namespace@H_502_41@std;@H_502_41@@H_502_41@
  7. @H_403_39@USING_NS_CC;@H_502_41@
  8. @H_403_39@@H_502_41@
  9. class@H_502_41@PathSprite:@H_502_41@public@H_502_41@cocos2d::Sprite@H_502_41@//继承Sprite类,因为要在里面加些其他变量@H_502_41@@H_502_41@@H_502_41@
  10. @H_403_39@{@H_502_41@
  11. @H_403_39@PathSprite():Sprite()@H_502_41@
  12. @H_403_39@m_parent=NULL;@H_502_41@
  13. @H_403_39@m_child=NULL;@H_502_41@
  14. @H_403_39@m_costToSource=0;@H_502_41@
  15. @H_403_39@m_FValue=0;@H_502_41@
  16. @H_403_39@};@H_502_41@
  17. @H_403_39@public@H_502_41@:@H_502_41@@H_502_41@
  18. static@H_502_41@PathSprite*create(@H_502_41@const@H_502_41@@H_502_41@char@H_502_41@*ch)@H_502_41@@H_502_41@
  19. @H_403_39@PathSprite*pRet=new@H_502_41@PathSprite();@H_502_41@@H_502_41@
  20. if@H_502_41@(pRet)@H_502_41@@H_502_41@
  21. @H_403_39@{@H_502_41@
  22. @H_403_39@pRet->initWithFile(ch);@H_502_41@
  23. @H_403_39@pRet->autorelease();@H_502_41@
  24. return@H_502_41@pRet;@H_502_41@@H_502_41@
  25. @H_403_39@}@H_502_41@
  26. else@H_502_41@@H_502_41@@H_502_41@
  27. delete@H_502_41@pRet;@H_502_41@@H_502_41@
  28. @H_403_39@pRet=NULL;@H_502_41@
  29. return@H_502_41@NULL;@H_502_41@@H_502_41@
  30. @H_403_39@}@H_502_41@
  31. @H_403_39@PathSprite*m_parent;//父节点@H_502_41@@H_502_41@@H_502_41@
  32. @H_403_39@PathSprite*m_child;//子节点@H_502_41@@H_502_41@@H_502_41@
  33. @H_403_39@float@H_502_41@m_costToSource;@H_502_41@//到起始点的距离@H_502_41@@H_502_41@@H_502_41@
  34. @H_403_39@int@H_502_41@m_x;@H_502_41@//地图坐标@H_502_41@@H_502_41@@H_502_41@
  35. int@H_502_41@m_y;@H_502_41@@H_502_41@
  36. float@H_502_41@m_FValue;@H_502_41@@H_502_41@
  37. class@H_502_41@PathSearchInfo@H_502_41@//寻路类(主要负责寻路的参数和逻辑)@H_502_41@@H_502_41@@H_502_41@
  38. public@H_502_41@:@H_502_41@@H_502_41@
  39. static@H_502_41@@H_502_41@int@H_502_41@m_startX;@H_502_41@//开始点@H_502_41@@H_502_41@@H_502_41@
  40. int@H_502_41@m_startY;@H_502_41@@H_502_41@
  41. int@H_502_41@m_endX;@H_502_41@//结束点@H_502_41@@H_502_41@@H_502_41@
  42. int@H_502_41@m_endY;@H_502_41@@H_502_41@
  43. static@H_502_41@vector<PathSprite*>m_openList;@H_502_41@//开放列表(里面存放相邻节点)@H_502_41@@H_502_41@@H_502_41@
  44. static@H_502_41@vector<PathSprite*>m_inspectList;@H_502_41@//检测列表(里面存放除了障碍物的节点)@H_502_41@@H_502_41@@H_502_41@
  45. static@H_502_41@vector<PathSprite*>m_pathList;@H_502_41@//路径列表@H_502_41@@H_502_41@@H_502_41@
  46. static@H_502_41@@H_502_41@void@H_502_41@barrierTest(vector<PathSprite*>&pathList,@H_502_41@int@H_502_41@x,87); font-weight:bold; background-color:inherit">int@H_502_41@y)@H_502_41@//模拟障碍物@H_502_41@@H_502_41@@H_502_41@
  47. @H_403_39@PathSprite*_z=getObjByPointOfMapCoord(pathList,x,y);@H_502_41@
  48. if@H_502_41@(_z)@H_502_41@@H_502_41@
  49. @H_403_39@_z->setColor(ccColor3B::MAGENTA);@H_502_41@
  50. @H_403_39@removeObjFromList(pathList,_z);@H_502_41@
  51. float@H_502_41@calculateTwoObjDistance(PathSprite*obj1,PathSprite*obj2)@H_502_41@//计算两个物体间的距离@H_502_41@@H_502_41@@H_502_41@
  52. @H_403_39@//float_offsetX=obj1->m_x-obj2->m_x;@H_502_41@@H_502_41@@H_502_41@
  53. @H_403_39@//float_offsetY=obj1->m_y-obj2->m_y;@H_502_41@@H_502_41@@H_502_41@
  54. //returnsqrt(_offsetX*_offsetX+_offsetY*_offsetY);@H_502_41@@H_502_41@@H_502_41@
  55. float@H_502_41@_x=abs(obj2->m_x-obj1->m_x);@H_502_41@@H_502_41@
  56. float@H_502_41@_y=abs(obj2->m_y-obj1->m_y);@H_502_41@@H_502_41@
  57. return@H_502_41@_x+_y;@H_502_41@@H_502_41@
  58. void@H_502_41@inspectTheAdjacentNodes(PathSprite*node,PathSprite*adjacent,PathSprite*endNode)@H_502_41@//把相邻的节点放入开放节点中@H_502_41@@H_502_41@@H_502_41@
  59. if@H_502_41@(adjacent)@H_502_41@@H_502_41@
  60. float@H_502_41@_x=abs(endNode->m_x-adjacent->m_x);@H_502_41@@H_502_41@
  61. float@H_502_41@_y=abs(endNode->m_y-adjacent->m_y);@H_502_41@@H_502_41@
  62. float@H_502_41@F,G,H1,H2,H3;@H_502_41@@H_502_41@
  63. @H_403_39@adjacent->m_costToSource=node->m_costToSource+calculateTwoObjDistance(node,adjacent);//获得累计的路程@H_502_41@@H_502_41@@H_502_41@
  64. @H_403_39@G=adjacent->m_costToSource;@H_502_41@
  65. //三种算法,感觉H2不错@H_502_41@@H_502_41@@H_502_41@
  66. @H_403_39@H1=_x+_y;@H_502_41@
  67. @H_403_39@H2=hypot(_x,_y);@H_502_41@
  68. @H_403_39@H3=max(_x,_y);@H_502_41@
  69. @H_403_39@#if1//A*算法=Dijkstra算法+最佳优先搜索@H_502_41@@H_502_41@@H_502_41@
  70. @H_403_39@F=G+H2;@H_502_41@
  71. @H_403_39@#endif@H_502_41@@H_502_41@@H_502_41@
  72. @H_403_39@#if0//Dijkstra算法@H_502_41@@H_502_41@@H_502_41@
  73. @H_403_39@F=G;@H_502_41@
  74. @H_403_39@#endif@H_502_41@@H_502_41@@H_502_41@
  75. @H_403_39@#if0//最佳优先搜索@H_502_41@@H_502_41@@H_502_41@
  76. @H_403_39@F=H2;@H_502_41@
  77. @H_403_39@adjacent->m_FValue=F;@H_502_41@
  78. @H_403_39@adjacent->m_parent=node;//设置父节点@H_502_41@@H_502_41@@H_502_41@
  79. @H_403_39@adjacent->setColor(Color3B::ORANGE);//搜寻过的节点设为橘色@H_502_41@@H_502_41@@H_502_41@
  80. @H_403_39@node->m_child=adjacent;//设置子节点@H_502_41@@H_502_41@@H_502_41@
  81. @H_403_39@PathSearchInfo::removeObjFromList(PathSearchInfo::m_inspectList,0); background-color:inherit">//把检测过的点从检测列表中删除@H_502_41@@H_502_41@@H_502_41@
  82. @H_403_39@PathSearchInfo::m_openList.push_back(adjacent);//加入开放列表@H_502_41@@H_502_41@@H_502_41@
  83. static@H_502_41@PathSprite*getMinPathFormOpenList()@H_502_41@//从开放节点中获取路径最小值@H_502_41@@H_502_41@@H_502_41@
  84. if@H_502_41@(m_openList.size()>0){@H_502_41@@H_502_41@
  85. @H_403_39@PathSprite*_sp=*m_openList.begin();@H_502_41@
  86. for@H_502_41@(vector<PathSprite*>::iteratoriter=m_openList.begin();iter!=m_openList.end();iter++)@H_502_41@@H_502_41@
  87. if@H_502_41@((*iter)->m_FValue<_sp->m_FValue)@H_502_41@@H_502_41@
  88. @H_403_39@_sp=*iter;@H_502_41@
  89. return@H_502_41@_sp;@H_502_41@@H_502_41@
  90. static@H_502_41@PathSprite*getObjByPointOfMapCoord(vector<PathSprite*>&spriteVector,0); background-color:inherit">//根据点获取对象@H_502_41@@H_502_41@@H_502_41@
  91. for@H_502_41@(@H_502_41@int@H_502_41@i=0;i<spriteVector.size();i++)@H_502_41@@H_502_41@
  92. if@H_502_41@(spriteVector[i]->m_x==x&&spriteVector[i]->m_y==y)@H_502_41@@H_502_41@
  93. return@H_502_41@spriteVector[i];@H_502_41@@H_502_41@
  94. return@H_502_41@NULL;@H_502_41@@H_502_41@
  95. bool@H_502_41@removeObjFromList(vector<PathSprite*>&spriteVector,PathSprite*sprite)@H_502_41@//从容器中移除对象@H_502_41@@H_502_41@@H_502_41@
  96. for@H_502_41@(vector<PathSprite*>::iteratoriter=spriteVector.begin();iter!=spriteVector.end();iter++)@H_502_41@@H_502_41@
  97. if@H_502_41@(*iter==sprite)@H_502_41@@H_502_41@
  98. @H_403_39@spriteVector.erase(iter);@H_502_41@
  99. return@H_502_41@@H_502_41@true@H_502_41@;@H_502_41@@H_502_41@
  100. false@H_502_41@;@H_502_41@@H_502_41@
  101. @H_403_39@};@H_502_41@
  102. class@H_502_41@HelloWorld:@H_502_41@public@H_502_41@cocos2d::Layer@H_502_41@@H_502_41@
  103. //there'sno'id'incpp,sowerecommendreturningtheclassinstancepointer@H_502_41@@H_502_41@@H_502_41@
  104. static@H_502_41@cocos2d::Scene*createScene();@H_502_41@@H_502_41@
  105. //Here'sadifference.Method'init'incocos2d-xreturnsbool,insteadofreturning'id'incocos2d-iphone@H_502_41@@H_502_41@@H_502_41@
  106. virtual@H_502_41@@H_502_41@bool@H_502_41@init();@H_502_41@@H_502_41@
  107. //aselectorcallback@H_502_41@@H_502_41@@H_502_41@
  108. void@H_502_41@menuCloseCallback(cocos2d::Ref*pSender);@H_502_41@@H_502_41@
  109. //implementthe"staticcreate()"methodmanually@H_502_41@@H_502_41@@H_502_41@
  110. @H_403_39@CREATE_FUNC(HelloWorld);@H_502_41@
  111. bool@H_502_41@onTouchBegan(Touch*touch,Event*event);@H_502_41@@H_502_41@
  112. void@H_502_41@onTouchMoved(Touch*touch,Event*event);@H_502_41@@H_502_41@
  113. void@H_502_41@onTouchEnded(Touch*touch,153); font-weight:bold; background-color:inherit">void@H_502_41@calculatePath();@H_502_41@//计算路径@H_502_41@@H_502_41@@H_502_41@
  114. void@H_502_41@drawPath();@H_502_41@//绘制路径@H_502_41@@H_502_41@@H_502_41@
  115. @H_403_39@vector<PathSprite*>m_mapList;//地图@H_502_41@@H_502_41@@H_502_41@
  116. void@H_502_41@clearPath();@H_502_41@//清理路径@H_502_41@@H_502_41@@H_502_41@
  117. @H_403_39@PathSprite*m_player;//人物用于演示行走@H_502_41@@H_502_41@@H_502_41@
  118. int@H_502_41@m_playerMoveStep;@H_502_41@//人物当前的行程@H_502_41@@H_502_41@@H_502_41@
  119. void@H_502_41@playerMove();@H_502_41@//人物走动@H_502_41@@H_502_41@@H_502_41@
  120. @H_403_39@#endif//__HELLOWORLD_SCENE_H__@H_502_41@@H_502_41@@H_502_41@
    @H_403_39@#include"HelloWorldScene.h"@H_502_41@@H_502_41@@H_502_41@
  1. @H_403_39@vector<PathSprite*>PathSearchInfo::m_openList;@H_502_41@
  2. @H_403_39@vector<PathSprite*>PathSearchInfo::m_inspectList;@H_502_41@
  3. @H_403_39@vector<PathSprite*>PathSearchInfo::m_pathList;@H_502_41@
  4. int@H_502_41@PathSearchInfo::m_startX;@H_502_41@@H_502_41@
  5. int@H_502_41@PathSearchInfo::m_startY;@H_502_41@@H_502_41@
  6. int@H_502_41@PathSearchInfo::m_endX;@H_502_41@@H_502_41@
  7. int@H_502_41@PathSearchInfo::m_endY;@H_502_41@@H_502_41@
  8. @H_403_39@Scene*HelloWorld::createScene()@H_502_41@
  9. //'scene'isanautoreleaSEObject@H_502_41@@H_502_41@@H_502_41@
  10. @H_403_39@autoscene=Scene::create();@H_502_41@
  11. //'layer'isanautoreleaSEObject@H_502_41@@H_502_41@@H_502_41@
  12. @H_403_39@autolayer=HelloWorld::create();@H_502_41@
  13. //addlayerasachildtoscene@H_502_41@@H_502_41@@H_502_41@
  14. @H_403_39@scene->addChild(layer);@H_502_41@
  15. //returnthescene@H_502_41@@H_502_41@@H_502_41@
  16. return@H_502_41@scene;@H_502_41@@H_502_41@
  17. //on"init"youneedtoinitializeyourinstance@H_502_41@@H_502_41@@H_502_41@
  18. bool@H_502_41@HelloWorld::init()@H_502_41@@H_502_41@
  19. //////////////////////////////@H_502_41@@H_502_41@@H_502_41@
  20. //1.superinitfirst@H_502_41@@H_502_41@@H_502_41@
  21. if@H_502_41@(!Layer::init())@H_502_41@@H_502_41@
  22. false@H_502_41@;@H_502_41@@H_502_41@
  23. @H_403_39@SizevisibleSize=Director::getInstance()->getVisibleSize();@H_502_41@
  24. @H_403_39@Vec2origin=Director::getInstance()->getVisibleOrigin();@H_502_41@
  25. @H_403_39@SizewinSize=Director::getInstance()->getWinSize();@H_502_41@
  26. /////////////////////////////@H_502_41@@H_502_41@@H_502_41@
  27. //2.addamenuitemwith"X"image,whichisclickedtoquittheprogram@H_502_41@@H_502_41@@H_502_41@
  28. //youmaymodifyit.@H_502_41@@H_502_41@@H_502_41@
  29. //adda"close"icontoexittheprogress.it'sanautoreleaSEObject@H_502_41@@H_502_41@@H_502_41@
  30. @H_403_39@autolistener=EventListenerTouchOneByOne::create();@H_502_41@
  31. @H_403_39@listener->setSwallowTouches(true@H_502_41@);@H_502_41@@H_502_41@
  32. @H_403_39@listener->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,this@H_502_41@);@H_502_41@@H_502_41@
  33. @H_403_39@listener->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,153); font-weight:bold; background-color:inherit">this@H_502_41@);@H_502_41@@H_502_41@
  34. @H_403_39@listener->onTouchEnded=CC_CALLBACK_2(HelloWorld::onTouchEnded,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> @H_403_39@_eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,0); background-color:inherit">//模拟一张地图左上角为(0,0)主要是模拟tiledmap每块的宽度为1@H_502_41@@H_502_41@@H_502_41@
  35. int@H_502_41@_width=25;@H_502_41@@H_502_41@
  36. int@H_502_41@_heigth=15;@H_502_41@@H_502_41@
  37. int@H_502_41@i=0;i<_heigth;i++)@H_502_41@@H_502_41@
  38. int@H_502_41@j=0;j<_width;j++)@H_502_41@@H_502_41@
  39. @H_403_39@PathSprite*_sp=PathSprite::create("CloseNormal.png"@H_502_41@);@H_502_41@@H_502_41@
  40. @H_403_39@_sp->m_x=j;@H_502_41@
  41. @H_403_39@_sp->m_y=i;@H_502_41@
  42. @H_403_39@Size_size=_sp->getContentSize();@H_502_41@
  43. @H_403_39@_sp->setPosition(CCPoint(j*_size.width+100,-i*_size.height+600));@H_502_41@
  44. @H_403_39@m_mapList.push_back(_sp);@H_502_41@
  45. this@H_502_41@->addChild(_sp);@H_502_41@@H_502_41@
  46. //设置障碍物@H_502_41@@H_502_41@@H_502_41@
  47. //for(inti=0;i<_heigth*_width/2;i++)@H_502_41@@H_502_41@@H_502_41@
  48. //{@H_502_41@@H_502_41@@H_502_41@
  49. //@H_502_41@@H_502_41@@H_502_41@
  50. //int_x=CCRANDOM_0_1()*_width;@H_502_41@@H_502_41@@H_502_41@
  51. //int_y=CCRANDOM_0_1()*_heigth;@H_502_41@@H_502_41@@H_502_41@
  52. //if(_x==0&&_y==0){@H_502_41@@H_502_41@@H_502_41@
  53. //continue;@H_502_41@@H_502_41@@H_502_41@
  54. //}@H_502_41@@H_502_41@@H_502_41@
  55. //PathSearchInfo::barrierTest(m_mapList,_x,_y);@H_502_41@@H_502_41@@H_502_41@
  56. int@H_502_41@i=0;i<10;i++){@H_502_41@@H_502_41@
  57. @H_403_39@PathSearchInfo::barrierTest(m_mapList,5+i,10);@H_502_41@
  58. @H_403_39@PathSearchInfo::barrierTest(m_mapList,15,i+1);@H_502_41@
  59. //PathSprite::getObjByPointOfMapCoord(m_inspectList,2,5)->removeFromParent();@H_502_41@@H_502_41@@H_502_41@
  60. //设置起始和终点@H_502_41@@H_502_41@@H_502_41@
  61. @H_403_39@PathSearchInfo::m_startX=0;@H_502_41@
  62. @H_403_39@PathSearchInfo::m_startY=0;@H_502_41@
  63. @H_403_39@PathSearchInfo::m_endX=4;@H_502_41@
  64. @H_403_39@PathSearchInfo::m_endY=9;@H_502_41@
  65. @H_403_39@m_player=PathSprite::create("CloseSelected1.png"@H_502_41@);@H_502_41@@H_502_41@
  66. @H_403_39@m_player->setColor(Color3B::RED);@H_502_41@
  67. this@H_502_41@->addChild(m_player);@H_502_41@@H_502_41@
  68. @H_403_39@m_player->m_x=PathSearchInfo::m_startX;@H_502_41@
  69. @H_403_39@m_player->m_y=PathSearchInfo::m_startY;@H_502_41@
  70. @H_403_39@m_player->setPosition(PathSearchInfo::getObjByPointOfMapCoord(m_mapList,PathSearchInfo::m_startX,PathSearchInfo::m_startY)->getPosition());@H_502_41@
  71. true@H_502_41@;@H_502_41@@H_502_41@
  72. void@H_502_41@HelloWorld::calculatePath()@H_502_41@@H_502_41@
  73. //得到开始点的节点@H_502_41@@H_502_41@@H_502_41@
  74. @H_403_39@PathSprite*_sp=PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList,PathSearchInfo::m_startY);@H_502_41@
  75. @H_403_39@PathSprite*_endNode=PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList,PathSearchInfo::m_endX,PathSearchInfo::m_endY);@H_502_41@
  76. //因为是开始点把到起始点的距离设为0@H_502_41@@H_502_41@@H_502_41@
  77. @H_403_39@_sp->m_costToSource=0;@H_502_41@
  78. @H_403_39@_sp->m_FValue=0;@H_502_41@
  79. //把已经检测过的点从检测列表中删除@H_502_41@@H_502_41@@H_502_41@
  80. 502_41@
  81. //然后加入开放列表@H_502_41@@H_502_41@@H_502_41@
  82. @H_403_39@PathSearchInfo::m_openList.push_back(_sp);@H_502_41@
  83. @H_403_39@PathSprite*_node=NULL;@H_502_41@
  84. while@H_502_41@(@H_502_41@true@H_502_41@)@H_502_41@@H_502_41@
  85. //得到离起始点最近的点@H_502_41@@H_502_41@@H_502_41@
  86. @H_403_39@_node=PathSearchInfo::getMinPathFormOpenList();@H_502_41@
  87. if@H_502_41@(!_node)@H_502_41@@H_502_41@
  88. //找不到路径@H_502_41@@H_502_41@@H_502_41@
  89. break@H_502_41@;@H_502_41@@H_502_41@
  90. //把计算过的点从开放列表中删除@H_502_41@@H_502_41@@H_502_41@
  91. @H_403_39@PathSearchInfo::removeObjFromList(PathSearchInfo::m_openList,_node);@H_502_41@
  92. int@H_502_41@_x=_node->m_x;@H_502_41@@H_502_41@
  93. int@H_502_41@_y=_node->m_y;@H_502_41@@H_502_41@
  94. if@H_502_41@(_x==PathSearchInfo::m_endX&&_y==PathSearchInfo::m_endY)@H_502_41@@H_502_41@
  95. //检测8个方向的相邻节点是否可以放入开放列表中@H_502_41@@H_502_41@@H_502_41@
  96. @H_403_39@PathSprite*_adjacent=PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList,_x+1,_y+1);@H_502_41@
  97. @H_403_39@PathSearchInfo::inspectTheAdjacentNodes(_node,_adjacent,_endNode);@H_502_41@
  98. @H_403_39@_adjacent=PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList,248)"> @H_403_39@PathSearchInfo::inspectTheAdjacentNodes(_node,_endNode);@H_502_41@
  99. @H_403_39@_adjacent=PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList,_y-1);@H_502_41@
  100. 502_41@
  101. 502_41@
  102. while@H_502_41@(_node)@H_502_41@@H_502_41@
  103. //PathSprite*_sp=node;@H_502_41@@H_502_41@@H_502_41@
  104. @H_403_39@PathSearchInfo::m_pathList.insert(PathSearchInfo::m_pathList.begin(),248)"> @H_403_39@_node=_node->m_parent;@H_502_41@
  105. void@H_502_41@HelloWorld::drawPath()@H_502_41@@H_502_41@
  106. for@H_502_41@(vector<PathSprite*>::iteratoriter=PathSearchInfo::m_pathList.begin();iter!=PathSearchInfo::m_pathList.end();iter++)@H_502_41@@H_502_41@
  107. @H_403_39@(*iter)->setColor(ccColor3B::GREEN);@H_502_41@
  108. bool@H_502_41@HelloWorld::onTouchBegan(Touch*touch,Event*event)@H_502_41@@H_502_41@
  109. //清除之前的路径@H_502_41@@H_502_41@@H_502_41@
  110. @H_403_39@clearPath();@H_502_41@
  111. @H_403_39@autonodePosition=convertToNodeSpace(touch->getLocation());@H_502_41@
  112. @H_403_39@log("%f,%f"@H_502_41@,nodePosition.x,nodePosition.y);@H_502_41@@H_502_41@
  113. int@H_502_41@i=0;i<PathSearchInfo::m_inspectList.size();i++)@H_502_41@@H_502_41@
  114. @H_403_39@PathSprite*_sp=PathSearchInfo::m_inspectList[i];@H_502_41@
  115. if@H_502_41@(_sp->getBoundingBox().containsPoint(nodePosition))@H_502_41@@H_502_41@
  116. //获取触摸点,设置为终点@H_502_41@@H_502_41@@H_502_41@
  117. @H_403_39@PathSearchInfo::m_endX=_sp->m_x;@H_502_41@
  118. @H_403_39@PathSearchInfo::m_endY=_sp->m_y;@H_502_41@
  119. //计算路径@H_502_41@@H_502_41@@H_502_41@
  120. @H_403_39@calculatePath();@H_502_41@
  121. @H_403_39@drawPath();@H_502_41@
  122. @H_403_39@playerMove();@H_502_41@
  123. void@H_502_41@HelloWorld::onTouchMoved(Touch*touch,0); background-color:inherit">//Ifitweren'tfortheTouchDispatcher,youwouldneedtokeepareference@H_502_41@@H_502_41@@H_502_41@
  124. //tothetouchfromtouchBeganandcheckthatthecurrenttouchisthesame@H_502_41@@H_502_41@@H_502_41@
  125. //asthatone.@H_502_41@@H_502_41@@H_502_41@
  126. //Actually,itwouldbeevenmorecomplicatedsinceintheCocosdispatcher@H_502_41@@H_502_41@@H_502_41@
  127. //yougetSetsinsteadof1UITouch,soyou'dneedtoloopthroughtheset@H_502_41@@H_502_41@@H_502_41@
  128. //ineachtouchXXXmethod.@H_502_41@@H_502_41@@H_502_41@
  129. void@H_502_41@HelloWorld::onTouchEnded(Touch*touch,153); font-weight:bold; background-color:inherit">void@H_502_41@HelloWorld::menuCloseCallback(Ref*pSender)@H_502_41@@H_502_41@
  130. @H_403_39@#if(CC_TARGET_PLATFORM==CC_PLATFORM_WP8)||(CC_TARGET_PLATFORM==CC_PLATFORM_WINRT)@H_502_41@@H_502_41@@H_502_41@
  131. @H_403_39@MessageBox("Youpressedtheclosebutton.WindowsStoreAppsdonotimplementaclosebutton."@H_502_41@,@H_502_41@"Alert"@H_502_41@);@H_502_41@@H_502_41@
  132. return@H_502_41@;@H_502_41@@H_502_41@
  133. @H_403_39@Director::getInstance()->end();@H_502_41@
  134. @H_403_39@#if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)@H_502_41@@H_502_41@@H_502_41@
  135. @H_403_39@exit(0);@H_502_41@
  136. void@H_502_41@HelloWorld::clearPath()@H_502_41@@H_502_41@
  137. for@H_502_41@(vector<PathSprite*>::iteratoriter=m_mapList.begin();iter!=m_mapList.end();iter++)@H_502_41@@H_502_41@
  138. @H_403_39@(*iter)->setColor(ccColor3B::WHITE);@H_502_41@
  139. @H_403_39@(*iter)->m_costToSource=0;@H_502_41@
  140. @H_403_39@(*iter)->m_FValue=0;@H_502_41@
  141. @H_403_39@(*iter)->m_parent=NULL;@H_502_41@
  142. @H_403_39@(*iter)->m_child=NULL;@H_502_41@
  143. //把移除了障碍物的地图放入检测列表中@H_502_41@@H_502_41@@H_502_41@
  144. @H_403_39@PathSearchInfo::m_inspectList=m_mapList;@H_502_41@
  145. @H_403_39@PathSearchInfo::m_openList.clear();@H_502_41@
  146. @H_403_39@PathSearchInfo::m_pathList.clear();@H_502_41@
  147. @H_403_39@PathSearchInfo::m_startX=m_player->m_x;@H_502_41@
  148. @H_403_39@PathSearchInfo::m_startY=m_player->m_y;@H_502_41@
  149. @H_403_39@m_player->stopAllActions();@H_502_41@
  150. @H_403_39@m_playerMoveStep=0;@H_502_41@
  151. void@H_502_41@HelloWorld::playerMove()@H_502_41@@H_502_41@
  152. @H_403_39@m_playerMoveStep++;@H_502_41@
  153. if@H_502_41@(m_playerMoveStep>=PathSearchInfo::m_pathList.size()){@H_502_41@@H_502_41@
  154. @H_403_39@m_player->m_x=PathSearchInfo::m_pathList[m_playerMoveStep]->m_x;@H_502_41@
  155. @H_403_39@m_player->m_y=PathSearchInfo::m_pathList[m_playerMoveStep]->m_y;@H_502_41@
  156. @H_403_39@m_player->runAction(Sequence::create(MoveTo::create(0.2,PathSearchInfo::m_pathList[m_playerMoveStep]->getPosition()),CallFunc::create(this@H_502_41@,SEL_CallFunc(&HelloWorld::playerMove)),NULL));@H_502_41@@H_502_41@
  157. @H_403_39@}@H_502_41@

@H_502_41@
转载自http://blog.csdn.net/w18767104183/article/details/39650409@H_502_41@

猜你在找的Cocos2d-x相关文章