首页 > 代码库 > cocos2dx A* + tiledMap

cocos2dx A* + tiledMap

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

这章中讲 A*结合tiledmap

先看下效果图


图有点丑,忍受下

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

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

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


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


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

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

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

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

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "vector"
using namespace std;
USING_NS_CC;
#define MAP_WIDTH 200//要比tmx中的map大
#define MAP_HEIGHT 200
class PathSprite 
{
public:
    PathSprite(CCSprite* sprite)
    {
        m_parent = NULL;
        m_child = NULL;
        m_costToSource = 0;
        m_FValue = http://www.mamicode.com/0;>
#include "HelloWorldScene.h"

USING_NS_CC;
vector<PathSprite*> PathSearchInfo::m_openList;

PathSprite* PathSearchInfo::m_inspectArray[MAP_WIDTH][MAP_HEIGHT] = {NULL};

vector<PathSprite*> PathSearchInfo::m_pathList;

vector<PathSprite*> PathSearchInfo::m_haveInspectList;

CCSize PathSearchInfo::m_mapSize;

CCSize PathSearchInfo::m_tileSize;

int PathSearchInfo::m_startX;

int PathSearchInfo::m_startY;

int PathSearchInfo::m_endX;

int PathSearchInfo::m_endY;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
void HelloWorld::onEnter()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    CCLayer::onEnter();

}

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.


    
    CCLabelTTF* pLabel = CCLabelTTF::create("A* + tiledMap", "Arial", 24);
    
    // position the label on the center of the screen
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    this->scheduleUpdate();

   CCTMXTiledMap* map = CCTMXTiledMap::create("gameMap.tmx");
    this->addChild(map);
    map->setPosition(CCPoint());
    CCTMXLayer* _road = map->layerNamed("road");//行走路径的地图
    CCSize _mapSize = map->getMapSize();
    for (int j = 0;  j < _mapSize.height; j++) {
        for (int i = 0;  i < _mapSize.width; i++) {
            CCSprite* _sp = _road->tileAt(CCPoint(i, j));
            if (_sp) {
                PathSprite* _pathSprite = new PathSprite(_sp);
                _pathSprite->m_x = i;
                _pathSprite->m_y = j;
                PathSearchInfo::m_inspectArray[i][j] = _pathSprite;//把地图中所有的点一一对应放入检测列表中
            }
        }
    }
    PathSearchInfo::m_mapSize = _mapSize;//获取地图的尺寸
    PathSearchInfo::m_tileSize = map->getTileSize();//获取瓦片的尺寸
    
    //设置起始和终点
    PathSearchInfo::m_startX =30;
    PathSearchInfo::m_startY = 75;

    //创建一个人物
    m_player = new PathSprite(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 = http://www.mamicode.com/0;>


cocos2dx A* + tiledMap