• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

cocos2dx A* + tiledMap

mysql 搞代码 4年前 (2022-01-09) 13次浏览 已收录 0个评论

前面一章讲了cocos2dx 中使用A星算法 这章中讲 A*结合tiledmap 先看下效果图 图有点丑,忍受下 绿色的块 表示人物的行走的路线(A*算法的结果) 红色部分 表示A*算法搜寻过的点(越少,速度越快) 黑色的部分(其实是无色块,因为背景是黑色的) 表示障碍物 这张图是

前面一章讲了cocos2dx 中使用A星算法

这章中讲 A*结合tiledmap

先看下效果图

图有点丑,忍受下

绿色的块%20表示人物的行走的路线(A*算法的结果)

红色部分%20表示A*算法搜寻过的点(越少,速度越快)

黑色的部分(其实是无色块,因为背景是黑色的)%20表示障碍物

这张图是用tiledmap做出来的,%20看看里面的内容

可以看到%20我把不能通过的地区的图块给删了

tiledmap中有2个层%20一个是background,%20一个是road.%20为了方便,%20我把road也用同样的图片,%20最好的方法是用一种同样的瓦片拼接出来一条能走的路,%20让后把background图层加到road图层上就ok了.

下面直接上源码,%20用的时cocos2.2.3,%20拷贝到项目中就能用了.当然别忘了自己做个像样的tiledMap%20.%20

%20如果你觉得好用,%20就在文章底下顶一个吧%20,%20enjoy%20it%20!

#ifndef%20__HELLOWORLD_SCENE_H__#define%20__HELLOWORLD_SCENE_H__#include%20"cocos2d.h"#include%20"vector"using%20namespace%20std;USING_NS_CC;#define%20MAP_WIDTH%20200//要比tmx中的map大#define%20MAP_HEIGHT%20200class%20PathSprite%20{public:%20%20%20%20PathSprite(CCSprite*%20sprite)%20%20%20%20{%20%20%20%20%20%20%20%20m_parent%20=%20NULL;%20%20%20%20%20%20%20%20m_child%20=%20NULL;%20%20%20%20%20%20%20%20m_costToSource%20=%200;%20%20%20%20%20%20%20%20m_FValue%20=%200;%20%20%20%20%20%20%20%20m_sprite%20=%20sprite;%20%20%20%20};public:%20%20%20%20CCSprite*%20m_sprite;//包含的瓦片精灵%20%20%20%20PathSprite*%20m_parent;//父节点%20%20%20%20PathSprite*%20m_child;//子节点%20%20%20%20float%20m_costToSource;//到起始点的距离%20%20%20%20int%20m_x;//地图坐标%20%20%20%20int%20m_y;%20%20%20%20float%20m_FValue;};class%20PathSearchInfo//寻路类(主要负责寻路的参数和逻辑){public:%20%20%20%20%20%20%20%20static%20int%20m_startX;//开始点%20%20%20%20static%20int%20m_startY;%20%20%20%20%20%20%20%20static%20int%20m_endX;//结束点%20%20%20%20static%20int%20m_endY;%20%20%20%20%20static%20CCSize%20m_mapSize;//地图大小%20%20%20%20static%20CCSize%20m_tileSize;//地图的块大小%20%20%20%20static%20vector%20m_openList;//开放列表(里面存放相邻节点)%20%20%20%20static%20PathSprite*%20m_inspectArray[MAP_WIDTH][MAP_HEIGHT];//全部需要检测的点%20%20%20%20static%20vector%20m_pathList;//路径列表%20%20%20%20static%20vector%20m_haveInspectList;//检测过的列表%20%20%20%20static%20float%20calculateTwoObjDistance(PathSprite*%20obj1,%20PathSprite*%20obj2)//计算两个物体间的距离%20%20%20%20{%20%20%20%20%20%20%20%20//%20%20%20%20%20%20%20%20float%20_offsetX%20=%20obj1->m_x%20-%20obj2->m_x;%20%20%20%20%20%20%20%20//%20%20%20%20%20%20%20%20float%20_offsetY%20=%20obj1->m_y%20-%20obj2->m_y;%20%20%20%20%20%20%20%20//%20%20%20%20%20%20%20%20return%20sqrt(%20_offsetX%20*%20_offsetX%20+%20_offsetY%20*%20_offsetY);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20float%20_x%20=%20abs(obj2->m_x%20-%20obj1->m_x);%20%20%20%20%20%20%20%20float%20_y%20=%20abs(obj2->m_y%20-%20obj1->m_y);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20_x%20+%20_y;%20%20%20%20}%20%20%20%20static%20void%20inspectTheAdjacentNodes(PathSprite*%20node,%20PathSprite*%20adjacent,%20PathSprite*%20endNode)//把相邻的节点放入开放节点中%20%20%20%20{%20%20%20%20%20%20%20%20if%20(adjacent)%20%20%20%20%20%20%20%20{%20%20%20%20%20%20%20%20%20%20%20%20float%20_x%20=%20abs(endNode->m_x%20-%20adjacent->m_x);%20%20%20%20%20%20%20%20%20%20%20%20float%20_y%20=%20abs(endNode->m_y%20-%20adjacent->m_y);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20float%20F%20,%20G,%20H1,%20H2,%20H3;%20%20%20%20%20%20%20%20%20%20%20%20adjacent->m_costToSource%20=%20node->m_costToSource%20+%20calculateTwoObjDistance(node,%20adjacent);//获得累计的路程%20%20%20%20%20%20%20%20%20%20%20%20G%20=%20adjacent->m_costToSource;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20//三种算法,%20感觉H2不错%20%20%20%20%20%20%20%20%20%20%20%20H1%20=%20_x%20+%20_y;%20%20%20%20%20%20%20%20%20%20%20%20H2%20=%20hypot(_x,%20_y);%20%20%20%20%20%20%20%20%20%20%20%20H3%20=%20max(_x,%20_y);%20%20%20%20%20%20%20%20%20%20%20%20#if%201%20//A*算法%20=%20Dijkstra算法%20+%20最佳优先搜索%20%20%20%20%20%20%20%20%20%20%20%20F%20=%20G%20+%20H2;#endif#if%200//Dijkstra算法%20%20%20%20%20%20%20%20%20%20%20%20F%20=%20G;#endif#if%200//最佳优先搜索%20%20%20%20%20%20%20%20%20%20%20%20F%20=%20H2;#endif%20%20%20%20%20%20%20%20%20%20%20%20adjacent->m_FValue%20=%20F;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adjacent->m_parent%20=%20node;//设置父节点%20%20%20%20%20%20%20%20%20%20%20%20adjacent->m_sprite->setColor(ccORANGE);//搜寻过的节点设为橘色(测试用)%20%20%20%20%20%20%20%20%20%20%20%20m_haveInspectList.push_back(adjacent);%20%20%20%20%20%20%20%20%20%20%20%20node->m_child%20=%20adjacent;//设置子节点%20%20%20%20%20%20%20%20%20%20%20%20PathSearchInfo::m_inspectArray[adjacent->m_x][adjacent->m_y]%20=%20NULL;//把检测过的点从检测列表中删除%20%20%20%20%20%20%20%20%20%20%20%20PathSearchInfo::m_openList.push_back(adjacent);//加入开放列表%20%20%20%20%20%20%20%20}%20%20%20%20}%20%20%20%20static%20PathSprite*%20getMinPathFormOpenList()//从开放节点中获取F值最小值的点%20%20%20%20{%20%20%20%20%20%20%20%20if%20(m_openList.size()>0)%20{%20%20%20%20%20%20%20%20%20%20%20%20PathSprite*%20_sp%20=*%20m_openList.begin();%20%20%20%20%20%20%20%20%20%20%20%20for%20(vector::iterator%20iter%20=%20m_openList.begin();%20iter%20!=%20%20m_openList.end();%20iter++)%20%20%20%20%20%20%20%20%20%20%20%20{%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20((*iter)->m_FValue%20<%20_sp->m_FValue)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20{%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_sp%20=%20*iter;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}%20%20%20%20%20%20%20%20%20%20%20%20}%20%20%20%20%20%20%20%20%20%20%20%20return%20_sp;%20%20%20%20%20%20%20%20}%20%20%20%20%20%20%20%20else%20%20%20%20%20%20%20%20{%20%20%20%20%20%20%20%20%20%20%20%20return%20NULL;%20%20%20%20%20%20%20%20}%20%20%20%20%20%20%20%20%20%20%20%20}%20%20%20%20static%20PathSprite*%20getObjFromInspectArray(int%20x,%20int%20y)//根据横纵坐标从检测数组中获取点%20%20%20%20{%20%20%20%20%20%20%20%20if%20(x%20>=0%20&&%20y%20>=0%20&&%20x%20<%20m_mapSize.width%20&&%20y%20<%20m_mapSize.height)%20{%20%20%20%20%20%20%20%20%20%20%20%20return%20m_inspectArray[x][y];%20%20%20%20%20%20%20%20}%20%20%20%20%20%20%20%20return%20%20NULL;%20%20%20%20}%20%20%20%20static%20bool%20removeObjFromOpenList(%20PathSprite*%20sprite)//从开放列表中移除对象%20%20%20%20{%20%20%20%20%20%20%20%20if%20(!sprite)%20{%20%20%20%20%20%20%20%20%20%20%20%20return%20%20false;%20%20%20%20%20%20%20%20}%20%20%20%20%20%20%20%20for%20(vector::iterator%20iter%20=%20m_openList.begin();%20iter%20!=%20%20m_openList.end();%20iter++)%20%20%20%20%20%20%20%20{%20%20%20%20%20%20%20%20%20%20%20%20if%20(*iter%20==%20sprite)%20%20%20%20%20%20%20%20%20%20%20%20{%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20m_openList.erase(iter);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true;%20%20%20%20%20%20%20%20%20%20%20%20}%20%20%20%20%20%20%20%20}%20%20%20%20%20%20%20%20return%20false;%20%20%20%20%20%20%20%20%20%20%20%20}%20%20};class%20HelloWorld%20:%20public%20cocos2d::CCLayer{public:%20%20%20%20//%20Here's%20a%20difference.%20Method%20'init'%20in%20cocos2d-x%20returns%20bool,%20instead%20of%20returning%20'id'%20in%20cocos2d-iphone%20%20%20%20virtual%20bool%20init();%20%20%20%20%20%20//%20there's%20no%20'id'%20in%20cpp,%20so%20we%20recommend%20returning%20the%20class%20instance%20pointer%20%20%20%20static%20cocos2d::CCScene*%20scene();%20%20%20%20%20%20%20%20//%20a%20selector%20callback%20%20%20%20void%20menuCloseCallback(CCObject*%20pSender);%20%20%20%20%20%20%20%20//%20implement%20the%20"static%20node()"%20method%20manually%20%20%20%20CREATE_FUNC(HelloWorld);%20%20%20%20void%20onEnter();%20%20%20%20virtual%20bool%20ccTouchBegan(CCTouch*%20touch,%20CCEvent*%20event);%20%20%20%20virtual%20void%20ccTouchMoved(CCTouch*%20touch,%20CCEvent*%20event);%20%20%20%20virtual%20void%20ccTouchEnded(CCTouch*%20touch,%20CCEvent*%20event);%20%20%20%20%20%20%20%20void%20calculatePath();//计算路径%20%20%20%20%20%20%20%20void%20drawPath();//绘制路径(测试用)%20%20%20%20%20%20%20%20v本文来源gaodaima#com搞(代@码$网6oid%20clearPath();//清理路径%20%20%20%20void%20playerMove();//人物走动%20%20%20%20void%20update(float%20dt);//跟新大地图(行走时,%20人不动,%20地图跟着人动);%20%20%20%20public:%20%20%20%20CCPoint%20m_orignPoint;//人物的起始点%20%20%20%20%20%20%20%20PathSprite*%20m_player;//人物点%20%20%20%20%20%20%20%20int%20m_playerMoveStep;//人物当前的行程的索引%20%20%20%20};#endif%20//%20__HELLOWORLD_SCENE_H__
#include%20"HelloWorldScene.h"USING_NS_CC;vector%20PathSearchInfo::m_openList;PathSprite*%20PathSearchInfo::m_inspectArray[MAP_WIDTH][MAP_HEIGHT]%20=%20{NULL};vector%20PathSearchInfo::m_pathList;vector%20PathSearchInfo::m_haveInspectList;CCSize%20PathSearchInfo::m_mapSize;CCSize%20PathSearchInfo::m_tileSize;int%20PathSearchInfo::m_startX;int%20PathSearchInfo::m_startY;int%20PathSearchInfo::m_endX;int%20PathSearchInfo::m_endY;CCScene*%20HelloWorld::scene(){%20%20%20%20//%20'scene'%20is%20an%20autorelease%20object%20%20%20%20CCScene%20*scene%20=%20CCScene::create();%20%20%20%20%20%20%20%20//%20'layer'%20is%20an%20autorelease%20object%20%20%20%20HelloWorld%20*layer%20=%20HelloWorld::create();%20%20%20%20//%20add%20layer%20as%20a%20child%20to%20scene%20%20%20%20scene->addChild(layer);%20%20%20%20//%20return%20the%20scene%20%20%20%20return%20scene;}//%20on%20"init"%20you%20need%20to%20initialize%20your%20instancevoid%20HelloWorld::onEnter(){%20%20%20%20CCDirector*%20pDirector%20=%20CCDirector::sharedDirector();%20%20%20%20pDirector->getTouchDispatcher()->addTargetedDelegate(this,%200,%20true);%20%20%20%20CCLayer::onEnter();}bool%20HelloWorld::init(){%20%20%20%20//////////////////////////////%20%20%20%20//%201.%20super%20init%20first%20%20%20%20if%20(%20!CCLayer::init()%20)%20%20%20%20{%20%20%20%20%20%20%20%20return%20false;%20%20%20%20}%20%20%20%20%20%20%20%20CCSize%20visibleSize%20=%20CCDirector::sharedDirector()->getVisibleSize();%20%20%20%20CCPoint%20origin%20=%20CCDirector::sharedDirector()->getVisibleOrigin();%20%20%20%20/////////////////////////////%20%20%20%20//%202.%20add%20a%20menu%20item%20with%20"X"%20image,%20which%20is%20clicked%20to%20quit%20the%20program%20%20%20%20//%20%20%20%20you%20may%20modify%20it.%20%20%20%20%20%20%20%20CCLabelTTF*%20pLabel%20=%20CCLabelTTF::create("A*%20+%20tiledMap",%20"Arial",%2024);%20%20%20%20%20%20%20%20//%20position%20the%20label%20on%20the%20center%20of%20the%20screen%20%20%20%20pLabel->setPosition(ccp(origin.x%20+%20visibleSize.width/2,%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20origin.y%20+%20visibleSize.height%20-%20pLabel->getContentSize().height));%20%20%20%20//%20add%20the%20label%20as%20a%20child%20to%20this%20layer%20%20%20%20this->addChild(pLabel,%201);%20%20%20%20this->scheduleUpdate();%20%20%20CCTMXTiledMap*%20map%20=%20CCTMXTiledMap::create("gameMap.tmx");%20%20%20%20this->addChild(map);%20%20%20%20map->setPosition(CCPoint());%20%20%20%20CCTMXLayer*%20_road%20=%20map->layerNamed("road");//行走路径的地图%20%20%20%20CCSize%20_mapSize%20=%20map->getMapSize();%20%20%20%20for%20(int%20j%20=%200;%20%20j%20<%20_mapSize.height;%20j++)%20{%20%20%20%20%20%20%20%20for%20(int%20i%20=%200;%20%20i%20<%20_mapSize.width;%20i++)%20{%20%20%20%20%20%20%20%20%20%20%20%20CCSprite*%20_sp%20=%20_road->tileAt(CCPoint(i,%20j));%20%20%20%20%20%20%20%20%20%20%20%20if%20(_sp)%20{%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20PathSprite*%20_pathSprite%20=%20new%20PathSprite(_sp);%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_pathSprite->m_x%20=%20i;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_pathSprite->m_y%20=%20j;%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20PathSearchInfo::m_inspectArray[i][j]%20=%20_pathSprite;//把地图中所有的点一一对应放入检测列表中%20%20%20%20%20%20%20%20%20%20%20%20}%20%20%20%20%20%20%20%20}%20%20%20%20}%20%20%20%20PathSearchInfo::m_mapSize%20=%20_mapSize;//获取地图的尺寸%20%20%20%20PathSearchInfo::m_tileSize%20=%20map->getTileSize();//获取瓦片的尺寸%20%20%20%20%20%20%20%20//设置起始和终点%20%20%20%20PathSearchInfo::m_startX%20=30;%20%20%20%20PathSearchInfo::m_startY%20=%2075;%20%20%20%20//创建一个人物%20%20%20%20m_player%20=%20new%20PathSprite(CCSprite::create("10001.png"));    m_player->m_sprite->setAnchorPoint(CCPoint(0.5,0));    this->addChild(m_player->m_sprite);        m_player->m_x = PathSearchInfo::m_startX;//设置人物的起始的地图坐标    m_player->m_y = PathSearchInfo::m_startY;        m_orignPoint = PathSearchInfo::m_inspectArray[PathSearchInfo::m_startX][PathSearchInfo::m_startY]->m_sprite->getPosition();    m_player->m_sprite->setPosition(m_orignPoint);//设置人物的起始的世界坐标            return true;}void HelloWorld::calculatePath(){        //得到开始点的节点    PathSprite* _startNode = PathSearchInfo::m_inspectArray[PathSearchInfo::m_startX][PathSearchInfo::m_startY];    //得到结束点的节点    PathSprite* _endNode = PathSearchInfo::m_inspectArray[PathSearchInfo::m_endX][PathSearchInfo::m_endY];        //因为是开始点 把到起始点的距离设为0, F值也为0    _startNode->m_costToSource = 0;    _startNode->m_FValue = 0;        //把已经检测过的点从检测列表中删除    PathSearchInfo::m_inspectArray[PathSearchInfo::m_startX][PathSearchInfo::m_startY] = NULL;    //把该点放入已经检测过点的列表中    PathSearchInfo::m_haveInspectList.push_back(_startNode);    //然后加入开放列表    PathSearchInfo::m_openList.push_back(_startNode);        PathSprite* _node = NULL;    while (true)    {        //得到离起始点最近的点(如果是第一次执行, 得到的是起点)        _node = PathSearchInfo::getMinPathFormOpenList();        if (!_node)        {            //找不到路径            break;        }        //把计算过的点从开放列表中删除        PathSearchInfo::removeObjFromOpenList( _node);        int _x = _node->m_x;        int _y = _node->m_y;                //        if (_x ==PathSearchInfo::m_endX && _y == PathSearchInfo::m_endY)        {            break;        }                //检测8个方向的相邻节点是否可以放入开放列表中        CCLog("%d, %d",_x, _y);        PathSprite* _adjacent = PathSearchInfo::getObjFromInspectArray( _x + 1, _y + 1);        PathSearchInfo::inspectTheAdjacentNodes(_node, _adjacent, _endNode);                _adjacent = PathSearchInfo::getObjFromInspectArray(  _x +1, _y);        PathSearchInfo::inspectTheAdjacentNodes(_node, _adjacent, _endNode);                _adjacent = PathSearchInfo::getObjFromInspectArray(  _x +1, _y-1);        PathSearchInfo::inspectTheAdjacentNodes(_node, _adjacent, _endNode);                _adjacent = PathSearchInfo::getObjFromInspectArray(  _x , _y -1);        PathSearchInfo::inspectTheAdjacentNodes(_node, _adjacent, _endNode);                _adjacent = PathSearchInfo::getObjFromInspectArray(  _x -1, _y - 1);        PathSearchInfo::inspectTheAdjacentNodes(_node, _adjacent, _endNode);                _adjacent = PathSearchInfo::getObjFromInspectArray(  _x -1, _y);        PathSearchInfo::inspectTheAdjacentNodes(_node, _adjacent, _endNode);                _adjacent = PathSearchInfo::getObjFromInspectArray(  _x -1, _y+1);        PathSearchInfo::inspectTheAdjacentNodes(_node, _adjacent, _endNode);                _adjacent = PathSearchInfo::getObjFromInspectArray(  _x , _y+1);        PathSearchInfo::inspectTheAdjacentNodes(_node, _adjacent, _endNode);            }        while (_node)    {        //把路径点加入到路径列表中        PathSearchInfo::m_pathList.insert(PathSearchInfo::m_pathList.begin(), _node);        _node = _node->m_parent;    }}void HelloWorld::drawPath(  ){    for (vector::iterator iter = PathSearchInfo::m_pathList.begin(); iter !=  PathSearchInfo::m_pathList.end(); iter++)    {        (*iter)->m_sprite->setColor(ccGREEN);    }    }CCRect getBoundingBox(float x, float y, float width, float height){    return CCRect(x - width/2, y - height/2, width, height);}bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event){    //清除之前的路径    clearPath();        auto nodePosition = convertToNodeSpace( touch->getLocation() );    CCLog("%f, %f", nodePosition.x, nodePosition.y);//    for (int i = 0; i m_sprite->getPositionX(), _sp->m_sprite->getPositionY(), _sp->m_sprite->getContentSize().width, _sp->m_sprite->getContentSize().height);//        //        if (_rect.containsPoint(nodePosition))//        {    PathSprite* _sp = PathSearchInfo::m_inspectArray[(int)(nodePosition.x/PathSearchInfo::m_tileSize.width)][(int)(PathSearchInfo::m_mapSize.height - nodePosition.y/PathSearchInfo::m_tileSize.height)];    if (_sp) {        CCLog("%f, %f", _sp->m_sprite->getPositionX(), _sp->m_sprite->getPositionY());        //获取触摸点, 设置为终点        PathSearchInfo::m_endX = _sp->m_x;        PathSearchInfo::m_endY = _sp->m_y;        //计算路径        calculatePath();        //绘制路径        drawPath(  );        //移动物体        playerMove();    }        //        }//        //    }    return true;}void HelloWorld::ccTouchMoved(CCTouch* touch, CCEvent* event){}void HelloWorld::ccTouchEnded(CCTouch* touch, CCEvent* event){}void HelloWorld::menuCloseCallback(CCObject* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");#else    CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)    exit(0);#endif#endif}void HelloWorld::clearPath(){    for (vector::iterator iter = PathSearchInfo::m_haveInspectList.begin(); iter !=  PathSearchInfo::m_haveInspectList.end(); iter++)    {        (*iter)->m_sprite->setColor(ccWHITE);        (*iter)->m_costToSource = 0;        (*iter)->m_FValue = 0;        (*iter)->m_parent = NULL;        (*iter)->m_child = NULL;                PathSearchInfo::m_inspectArray[(*iter)->m_x][(*iter)->m_y] = (*iter);    }        //把移除了障碍物的地图放入检测列表中    //PathSearchInfo::m_inspectList = PathSearchInfo::m_mapList;    PathSearchInfo::m_openList.clear();    PathSearchInfo::m_pathList.clear();    PathSearchInfo::m_haveInspectList.clear();    PathSearchInfo::m_startX = m_player->m_x;    PathSearchInfo::m_startY = m_player->m_y;    m_player->m_sprite->stopAllActions();        m_playerMoveStep = 0;}void HelloWorld::playerMove(){    m_playerMoveStep++;        if (m_playerMoveStep >= PathSearchInfo::m_pathList.size()) {        return;    }        m_player->m_x = PathSearchInfo::m_pathList[m_playerMoveStep]->m_x;    m_player->m_y = PathSearchInfo::m_pathList[m_playerMoveStep]->m_y;        //根据路径列表移动人物    m_player->m_sprite->runAction(CCSequence::create(CCMoveTo::create(1/24.0, PathSearchInfo::m_pathList[m_playerMoveStep]->m_sprite->getPosition()), CCCallFunc::create(this, SEL_CallFunc(&HelloWorld::playerMove)) , NULL));    }void HelloWorld::update(float dt){    this->setPosition(m_orignPoint - m_player->m_sprite->getPosition());}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:cocos2dx A* + tiledMap

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址