使用cocos2dx 3.2 原理都一样
淡蓝色的点是地图
深蓝色的点是障碍物
绿色的点是路径
暗绿色的点是搜寻过的点
红色的点是按路径行走的点
dijkstra算法 会发现路径最短,但寻找过的路径比较多(计算速度慢)
最佳优先搜索算法会发现寻找过的路径少了(计算速度提高了),但走了许多弯路
A星算法 结合了上面2种算法 即寻找到了最短路径,搜寻过的路径也比较少
- @H_403_39@#ifndef__HELLOWORLD_SCENE_H__@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@#define__HELLOWORLD_SCENE_H__@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@@H_502_41@
- @H_403_39@#include"cocos2d.h"@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@#include"vector"@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@using@H_502_41@@H_502_41@namespace@H_502_41@std;@H_502_41@@H_502_41@
- @H_403_39@USING_NS_CC;@H_502_41@
- @H_403_39@@H_502_41@
- 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@
- @H_403_39@{@H_502_41@
- @H_403_39@PathSprite():Sprite()@H_502_41@
- @H_403_39@m_parent=NULL;@H_502_41@
- @H_403_39@m_child=NULL;@H_502_41@
- @H_403_39@m_costToSource=0;@H_502_41@
- @H_403_39@m_FValue=0;@H_502_41@
- @H_403_39@};@H_502_41@
- @H_403_39@public@H_502_41@:@H_502_41@@H_502_41@
- 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@
- @H_403_39@PathSprite*pRet=new@H_502_41@PathSprite();@H_502_41@@H_502_41@
- if@H_502_41@(pRet)@H_502_41@@H_502_41@
- @H_403_39@{@H_502_41@
- @H_403_39@pRet->initWithFile(ch);@H_502_41@
- @H_403_39@pRet->autorelease();@H_502_41@
- return@H_502_41@pRet;@H_502_41@@H_502_41@
- @H_403_39@}@H_502_41@
- else@H_502_41@@H_502_41@@H_502_41@
- delete@H_502_41@pRet;@H_502_41@@H_502_41@
- @H_403_39@pRet=NULL;@H_502_41@
- return@H_502_41@NULL;@H_502_41@@H_502_41@
- @H_403_39@}@H_502_41@
- @H_403_39@PathSprite*m_parent;//父节点@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSprite*m_child;//子节点@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@float@H_502_41@m_costToSource;@H_502_41@//到起始点的距离@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@int@H_502_41@m_x;@H_502_41@//地图坐标@H_502_41@@H_502_41@@H_502_41@
- int@H_502_41@m_y;@H_502_41@@H_502_41@
- float@H_502_41@m_FValue;@H_502_41@@H_502_41@
- class@H_502_41@PathSearchInfo@H_502_41@//寻路类(主要负责寻路的参数和逻辑)@H_502_41@@H_502_41@@H_502_41@
- public@H_502_41@:@H_502_41@@H_502_41@
- 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@
- int@H_502_41@m_startY;@H_502_41@@H_502_41@
- int@H_502_41@m_endX;@H_502_41@//结束点@H_502_41@@H_502_41@@H_502_41@
- int@H_502_41@m_endY;@H_502_41@@H_502_41@
- static@H_502_41@vector<PathSprite*>m_openList;@H_502_41@//开放列表(里面存放相邻节点)@H_502_41@@H_502_41@@H_502_41@
- static@H_502_41@vector<PathSprite*>m_inspectList;@H_502_41@//检测列表(里面存放除了障碍物的节点)@H_502_41@@H_502_41@@H_502_41@
- static@H_502_41@vector<PathSprite*>m_pathList;@H_502_41@//路径列表@H_502_41@@H_502_41@@H_502_41@
- 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@
- @H_403_39@PathSprite*_z=getObjByPointOfMapCoord(pathList,x,y);@H_502_41@
- if@H_502_41@(_z)@H_502_41@@H_502_41@
- @H_403_39@_z->setColor(ccColor3B::MAGENTA);@H_502_41@
- @H_403_39@removeObjFromList(pathList,_z);@H_502_41@
- float@H_502_41@calculateTwoObjDistance(PathSprite*obj1,PathSprite*obj2)@H_502_41@//计算两个物体间的距离@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@//float_offsetX=obj1->m_x-obj2->m_x;@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@//float_offsetY=obj1->m_y-obj2->m_y;@H_502_41@@H_502_41@@H_502_41@
- //returnsqrt(_offsetX*_offsetX+_offsetY*_offsetY);@H_502_41@@H_502_41@@H_502_41@
- float@H_502_41@_x=abs(obj2->m_x-obj1->m_x);@H_502_41@@H_502_41@
- float@H_502_41@_y=abs(obj2->m_y-obj1->m_y);@H_502_41@@H_502_41@
- return@H_502_41@_x+_y;@H_502_41@@H_502_41@
- void@H_502_41@inspectTheAdjacentNodes(PathSprite*node,PathSprite*adjacent,PathSprite*endNode)@H_502_41@//把相邻的节点放入开放节点中@H_502_41@@H_502_41@@H_502_41@
- if@H_502_41@(adjacent)@H_502_41@@H_502_41@
- float@H_502_41@_x=abs(endNode->m_x-adjacent->m_x);@H_502_41@@H_502_41@
- float@H_502_41@_y=abs(endNode->m_y-adjacent->m_y);@H_502_41@@H_502_41@
- float@H_502_41@F,G,H1,H2,H3;@H_502_41@@H_502_41@
- @H_403_39@adjacent->m_costToSource=node->m_costToSource+calculateTwoObjDistance(node,adjacent);//获得累计的路程@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@G=adjacent->m_costToSource;@H_502_41@
- //三种算法,感觉H2不错@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@H1=_x+_y;@H_502_41@
- @H_403_39@H2=hypot(_x,_y);@H_502_41@
- @H_403_39@H3=max(_x,_y);@H_502_41@
- @H_403_39@#if1//A*算法=Dijkstra算法+最佳优先搜索@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@F=G+H2;@H_502_41@
- @H_403_39@#endif@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@#if0//Dijkstra算法@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@F=G;@H_502_41@
- @H_403_39@#endif@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@#if0//最佳优先搜索@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@F=H2;@H_502_41@
- @H_403_39@adjacent->m_FValue=F;@H_502_41@
- @H_403_39@adjacent->m_parent=node;//设置父节点@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@adjacent->setColor(Color3B::ORANGE);//搜寻过的节点设为橘色@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@node->m_child=adjacent;//设置子节点@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSearchInfo::removeObjFromList(PathSearchInfo::m_inspectList,0); background-color:inherit">//把检测过的点从检测列表中删除@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSearchInfo::m_openList.push_back(adjacent);//加入开放列表@H_502_41@@H_502_41@@H_502_41@
- static@H_502_41@PathSprite*getMinPathFormOpenList()@H_502_41@//从开放节点中获取路径最小值@H_502_41@@H_502_41@@H_502_41@
- if@H_502_41@(m_openList.size()>0){@H_502_41@@H_502_41@
- @H_403_39@PathSprite*_sp=*m_openList.begin();@H_502_41@
- for@H_502_41@(vector<PathSprite*>::iteratoriter=m_openList.begin();iter!=m_openList.end();iter++)@H_502_41@@H_502_41@
- if@H_502_41@((*iter)->m_FValue<_sp->m_FValue)@H_502_41@@H_502_41@
- @H_403_39@_sp=*iter;@H_502_41@
- return@H_502_41@_sp;@H_502_41@@H_502_41@
- static@H_502_41@PathSprite*getObjByPointOfMapCoord(vector<PathSprite*>&spriteVector,0); background-color:inherit">//根据点获取对象@H_502_41@@H_502_41@@H_502_41@
- for@H_502_41@(@H_502_41@int@H_502_41@i=0;i<spriteVector.size();i++)@H_502_41@@H_502_41@
- if@H_502_41@(spriteVector[i]->m_x==x&&spriteVector[i]->m_y==y)@H_502_41@@H_502_41@
- return@H_502_41@spriteVector[i];@H_502_41@@H_502_41@
- return@H_502_41@NULL;@H_502_41@@H_502_41@
- bool@H_502_41@removeObjFromList(vector<PathSprite*>&spriteVector,PathSprite*sprite)@H_502_41@//从容器中移除对象@H_502_41@@H_502_41@@H_502_41@
- for@H_502_41@(vector<PathSprite*>::iteratoriter=spriteVector.begin();iter!=spriteVector.end();iter++)@H_502_41@@H_502_41@
- if@H_502_41@(*iter==sprite)@H_502_41@@H_502_41@
- @H_403_39@spriteVector.erase(iter);@H_502_41@
- return@H_502_41@@H_502_41@true@H_502_41@;@H_502_41@@H_502_41@
- false@H_502_41@;@H_502_41@@H_502_41@
- @H_403_39@};@H_502_41@
- class@H_502_41@HelloWorld:@H_502_41@public@H_502_41@cocos2d::Layer@H_502_41@@H_502_41@
- //there'sno'id'incpp,sowerecommendreturningtheclassinstancepointer@H_502_41@@H_502_41@@H_502_41@
- static@H_502_41@cocos2d::Scene*createScene();@H_502_41@@H_502_41@
- //Here'sadifference.Method'init'incocos2d-xreturnsbool,insteadofreturning'id'incocos2d-iphone@H_502_41@@H_502_41@@H_502_41@
- virtual@H_502_41@@H_502_41@bool@H_502_41@init();@H_502_41@@H_502_41@
- //aselectorcallback@H_502_41@@H_502_41@@H_502_41@
- void@H_502_41@menuCloseCallback(cocos2d::Ref*pSender);@H_502_41@@H_502_41@
- //implementthe"staticcreate()"methodmanually@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@CREATE_FUNC(HelloWorld);@H_502_41@
- bool@H_502_41@onTouchBegan(Touch*touch,Event*event);@H_502_41@@H_502_41@
- void@H_502_41@onTouchMoved(Touch*touch,Event*event);@H_502_41@@H_502_41@
- 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@
- void@H_502_41@drawPath();@H_502_41@//绘制路径@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@vector<PathSprite*>m_mapList;//地图@H_502_41@@H_502_41@@H_502_41@
- void@H_502_41@clearPath();@H_502_41@//清理路径@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSprite*m_player;//人物用于演示行走@H_502_41@@H_502_41@@H_502_41@
- int@H_502_41@m_playerMoveStep;@H_502_41@//人物当前的行程@H_502_41@@H_502_41@@H_502_41@
- void@H_502_41@playerMove();@H_502_41@//人物走动@H_502_41@@H_502_41@@H_502_41@
- @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@
- @H_403_39@vector<PathSprite*>PathSearchInfo::m_openList;@H_502_41@
- @H_403_39@vector<PathSprite*>PathSearchInfo::m_inspectList;@H_502_41@
- @H_403_39@vector<PathSprite*>PathSearchInfo::m_pathList;@H_502_41@
- int@H_502_41@PathSearchInfo::m_startX;@H_502_41@@H_502_41@
- int@H_502_41@PathSearchInfo::m_startY;@H_502_41@@H_502_41@
- int@H_502_41@PathSearchInfo::m_endX;@H_502_41@@H_502_41@
- int@H_502_41@PathSearchInfo::m_endY;@H_502_41@@H_502_41@
- @H_403_39@Scene*HelloWorld::createScene()@H_502_41@
- //'scene'isanautoreleaSEObject@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@autoscene=Scene::create();@H_502_41@
- //'layer'isanautoreleaSEObject@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@autolayer=HelloWorld::create();@H_502_41@
- //addlayerasachildtoscene@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@scene->addChild(layer);@H_502_41@
- //returnthescene@H_502_41@@H_502_41@@H_502_41@
- return@H_502_41@scene;@H_502_41@@H_502_41@
- //on"init"youneedtoinitializeyourinstance@H_502_41@@H_502_41@@H_502_41@
- bool@H_502_41@HelloWorld::init()@H_502_41@@H_502_41@
- //////////////////////////////@H_502_41@@H_502_41@@H_502_41@
- //1.superinitfirst@H_502_41@@H_502_41@@H_502_41@
- if@H_502_41@(!Layer::init())@H_502_41@@H_502_41@
- false@H_502_41@;@H_502_41@@H_502_41@
- @H_403_39@SizevisibleSize=Director::getInstance()->getVisibleSize();@H_502_41@
- @H_403_39@Vec2origin=Director::getInstance()->getVisibleOrigin();@H_502_41@
- @H_403_39@SizewinSize=Director::getInstance()->getWinSize();@H_502_41@
- /////////////////////////////@H_502_41@@H_502_41@@H_502_41@
- //2.addamenuitemwith"X"image,whichisclickedtoquittheprogram@H_502_41@@H_502_41@@H_502_41@
- //youmaymodifyit.@H_502_41@@H_502_41@@H_502_41@
- //adda"close"icontoexittheprogress.it'sanautoreleaSEObject@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@autolistener=EventListenerTouchOneByOne::create();@H_502_41@
- @H_403_39@listener->setSwallowTouches(true@H_502_41@);@H_502_41@@H_502_41@
- @H_403_39@listener->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,this@H_502_41@);@H_502_41@@H_502_41@
- @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@
- @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@
- int@H_502_41@_width=25;@H_502_41@@H_502_41@
- int@H_502_41@_heigth=15;@H_502_41@@H_502_41@
- int@H_502_41@i=0;i<_heigth;i++)@H_502_41@@H_502_41@
- int@H_502_41@j=0;j<_width;j++)@H_502_41@@H_502_41@
- @H_403_39@PathSprite*_sp=PathSprite::create("CloseNormal.png"@H_502_41@);@H_502_41@@H_502_41@
- @H_403_39@_sp->m_x=j;@H_502_41@
- @H_403_39@_sp->m_y=i;@H_502_41@
- @H_403_39@Size_size=_sp->getContentSize();@H_502_41@
- @H_403_39@_sp->setPosition(CCPoint(j*_size.width+100,-i*_size.height+600));@H_502_41@
- @H_403_39@m_mapList.push_back(_sp);@H_502_41@
- this@H_502_41@->addChild(_sp);@H_502_41@@H_502_41@
- //设置障碍物@H_502_41@@H_502_41@@H_502_41@
- //for(inti=0;i<_heigth*_width/2;i++)@H_502_41@@H_502_41@@H_502_41@
- //{@H_502_41@@H_502_41@@H_502_41@
- //@H_502_41@@H_502_41@@H_502_41@
- //int_x=CCRANDOM_0_1()*_width;@H_502_41@@H_502_41@@H_502_41@
- //int_y=CCRANDOM_0_1()*_heigth;@H_502_41@@H_502_41@@H_502_41@
- //if(_x==0&&_y==0){@H_502_41@@H_502_41@@H_502_41@
- //continue;@H_502_41@@H_502_41@@H_502_41@
- //}@H_502_41@@H_502_41@@H_502_41@
- //PathSearchInfo::barrierTest(m_mapList,_x,_y);@H_502_41@@H_502_41@@H_502_41@
- int@H_502_41@i=0;i<10;i++){@H_502_41@@H_502_41@
- @H_403_39@PathSearchInfo::barrierTest(m_mapList,5+i,10);@H_502_41@
- @H_403_39@PathSearchInfo::barrierTest(m_mapList,15,i+1);@H_502_41@
- //PathSprite::getObjByPointOfMapCoord(m_inspectList,2,5)->removeFromParent();@H_502_41@@H_502_41@@H_502_41@
- //设置起始和终点@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSearchInfo::m_startX=0;@H_502_41@
- @H_403_39@PathSearchInfo::m_startY=0;@H_502_41@
- @H_403_39@PathSearchInfo::m_endX=4;@H_502_41@
- @H_403_39@PathSearchInfo::m_endY=9;@H_502_41@
- @H_403_39@m_player=PathSprite::create("CloseSelected1.png"@H_502_41@);@H_502_41@@H_502_41@
- @H_403_39@m_player->setColor(Color3B::RED);@H_502_41@
- this@H_502_41@->addChild(m_player);@H_502_41@@H_502_41@
- @H_403_39@m_player->m_x=PathSearchInfo::m_startX;@H_502_41@
- @H_403_39@m_player->m_y=PathSearchInfo::m_startY;@H_502_41@
- @H_403_39@m_player->setPosition(PathSearchInfo::getObjByPointOfMapCoord(m_mapList,PathSearchInfo::m_startX,PathSearchInfo::m_startY)->getPosition());@H_502_41@
- true@H_502_41@;@H_502_41@@H_502_41@
- void@H_502_41@HelloWorld::calculatePath()@H_502_41@@H_502_41@
- //得到开始点的节点@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSprite*_sp=PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList,PathSearchInfo::m_startY);@H_502_41@
- @H_403_39@PathSprite*_endNode=PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList,PathSearchInfo::m_endX,PathSearchInfo::m_endY);@H_502_41@
- //因为是开始点把到起始点的距离设为0@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@_sp->m_costToSource=0;@H_502_41@
- @H_403_39@_sp->m_FValue=0;@H_502_41@
- //把已经检测过的点从检测列表中删除@H_502_41@@H_502_41@@H_502_41@
- 502_41@
- //然后加入开放列表@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSearchInfo::m_openList.push_back(_sp);@H_502_41@
- @H_403_39@PathSprite*_node=NULL;@H_502_41@
- while@H_502_41@(@H_502_41@true@H_502_41@)@H_502_41@@H_502_41@
- //得到离起始点最近的点@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@_node=PathSearchInfo::getMinPathFormOpenList();@H_502_41@
- if@H_502_41@(!_node)@H_502_41@@H_502_41@
- //找不到路径@H_502_41@@H_502_41@@H_502_41@
- break@H_502_41@;@H_502_41@@H_502_41@
- //把计算过的点从开放列表中删除@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSearchInfo::removeObjFromList(PathSearchInfo::m_openList,_node);@H_502_41@
- int@H_502_41@_x=_node->m_x;@H_502_41@@H_502_41@
- int@H_502_41@_y=_node->m_y;@H_502_41@@H_502_41@
- if@H_502_41@(_x==PathSearchInfo::m_endX&&_y==PathSearchInfo::m_endY)@H_502_41@@H_502_41@
- //检测8个方向的相邻节点是否可以放入开放列表中@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSprite*_adjacent=PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList,_x+1,_y+1);@H_502_41@
- @H_403_39@PathSearchInfo::inspectTheAdjacentNodes(_node,_adjacent,_endNode);@H_502_41@
- @H_403_39@_adjacent=PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList,248)"> @H_403_39@PathSearchInfo::inspectTheAdjacentNodes(_node,_endNode);@H_502_41@
- @H_403_39@_adjacent=PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList,_y-1);@H_502_41@
- 502_41@
- 502_41@
- while@H_502_41@(_node)@H_502_41@@H_502_41@
- //PathSprite*_sp=node;@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSearchInfo::m_pathList.insert(PathSearchInfo::m_pathList.begin(),248)"> @H_403_39@_node=_node->m_parent;@H_502_41@
- void@H_502_41@HelloWorld::drawPath()@H_502_41@@H_502_41@
- for@H_502_41@(vector<PathSprite*>::iteratoriter=PathSearchInfo::m_pathList.begin();iter!=PathSearchInfo::m_pathList.end();iter++)@H_502_41@@H_502_41@
- @H_403_39@(*iter)->setColor(ccColor3B::GREEN);@H_502_41@
- bool@H_502_41@HelloWorld::onTouchBegan(Touch*touch,Event*event)@H_502_41@@H_502_41@
- //清除之前的路径@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@clearPath();@H_502_41@
- @H_403_39@autonodePosition=convertToNodeSpace(touch->getLocation());@H_502_41@
- @H_403_39@log("%f,%f"@H_502_41@,nodePosition.x,nodePosition.y);@H_502_41@@H_502_41@
- int@H_502_41@i=0;i<PathSearchInfo::m_inspectList.size();i++)@H_502_41@@H_502_41@
- @H_403_39@PathSprite*_sp=PathSearchInfo::m_inspectList[i];@H_502_41@
- if@H_502_41@(_sp->getBoundingBox().containsPoint(nodePosition))@H_502_41@@H_502_41@
- //获取触摸点,设置为终点@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSearchInfo::m_endX=_sp->m_x;@H_502_41@
- @H_403_39@PathSearchInfo::m_endY=_sp->m_y;@H_502_41@
- //计算路径@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@calculatePath();@H_502_41@
- @H_403_39@drawPath();@H_502_41@
- @H_403_39@playerMove();@H_502_41@
- void@H_502_41@HelloWorld::onTouchMoved(Touch*touch,0); background-color:inherit">//Ifitweren'tfortheTouchDispatcher,youwouldneedtokeepareference@H_502_41@@H_502_41@@H_502_41@
- //tothetouchfromtouchBeganandcheckthatthecurrenttouchisthesame@H_502_41@@H_502_41@@H_502_41@
- //asthatone.@H_502_41@@H_502_41@@H_502_41@
- //Actually,itwouldbeevenmorecomplicatedsinceintheCocosdispatcher@H_502_41@@H_502_41@@H_502_41@
- //yougetSetsinsteadof1UITouch,soyou'dneedtoloopthroughtheset@H_502_41@@H_502_41@@H_502_41@
- //ineachtouchXXXmethod.@H_502_41@@H_502_41@@H_502_41@
- 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@
- @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@
- @H_403_39@MessageBox("Youpressedtheclosebutton.WindowsStoreAppsdonotimplementaclosebutton."@H_502_41@,@H_502_41@"Alert"@H_502_41@);@H_502_41@@H_502_41@
- return@H_502_41@;@H_502_41@@H_502_41@
- @H_403_39@Director::getInstance()->end();@H_502_41@
- @H_403_39@#if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@exit(0);@H_502_41@
- void@H_502_41@HelloWorld::clearPath()@H_502_41@@H_502_41@
- for@H_502_41@(vector<PathSprite*>::iteratoriter=m_mapList.begin();iter!=m_mapList.end();iter++)@H_502_41@@H_502_41@
- @H_403_39@(*iter)->setColor(ccColor3B::WHITE);@H_502_41@
- @H_403_39@(*iter)->m_costToSource=0;@H_502_41@
- @H_403_39@(*iter)->m_FValue=0;@H_502_41@
- @H_403_39@(*iter)->m_parent=NULL;@H_502_41@
- @H_403_39@(*iter)->m_child=NULL;@H_502_41@
- //把移除了障碍物的地图放入检测列表中@H_502_41@@H_502_41@@H_502_41@
- @H_403_39@PathSearchInfo::m_inspectList=m_mapList;@H_502_41@
- @H_403_39@PathSearchInfo::m_openList.clear();@H_502_41@
- @H_403_39@PathSearchInfo::m_pathList.clear();@H_502_41@
- @H_403_39@PathSearchInfo::m_startX=m_player->m_x;@H_502_41@
- @H_403_39@PathSearchInfo::m_startY=m_player->m_y;@H_502_41@
- @H_403_39@m_player->stopAllActions();@H_502_41@
- @H_403_39@m_playerMoveStep=0;@H_502_41@
- void@H_502_41@HelloWorld::playerMove()@H_502_41@@H_502_41@
- @H_403_39@m_playerMoveStep++;@H_502_41@
- if@H_502_41@(m_playerMoveStep>=PathSearchInfo::m_pathList.size()){@H_502_41@@H_502_41@
- @H_403_39@m_player->m_x=PathSearchInfo::m_pathList[m_playerMoveStep]->m_x;@H_502_41@
- @H_403_39@m_player->m_y=PathSearchInfo::m_pathList[m_playerMoveStep]->m_y;@H_502_41@
- @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@
- @H_403_39@}@H_502_41@
@H_502_41@
转载自http://blog.csdn.net/w18767104183/article/details/39650409@H_502_41@