Tiled是一个简单直观的中文游戏地图编辑器,帮助创建属于你自己的游戏地图。风格类似mini版的photoshop。它可以用于制作多种类型的游戏引擎需要,而且支持使用插件读写map、增加用于引擎的map格式。
Tiled使用教程
/*打开Tiled软件,新建一个文件,设置宽度和高度,然后添加图块(下图),添加好了后
自己创建地图,然后保存为level01.tmx.
*/
/************************************************************************/

1.在程序中加载tmx文件
CCTMXTiledMap *map = CCTMXTiledMap::create("level01.tmx");
this->addChild(map);
2.使用对象层(设置一个固定的精灵起点)
命名为objects,

设置对象属性,
在代码中获取到X和Y坐标的值:
/*加载对象层*/
CCTMXObjectGroup *objGroup = map->objectGroupNamed("objects");
/*加载玩家坐标对象*/
CCDictionary* playerPointDic = objGroup->objectNamed("PlayerPoint");
float x = playerPointDic->valueForKey("x")->floatValue();
float y = playerPointDic->valueForKey("y")->floatValue();
/*设置玩家坐标*/
m_player->setPosition(ccp(x,y));
3.添加障碍物,Tiled障碍层的使用
命名barrier,
此时主角精灵还是能够越过这个障碍继续向前,还得建一个meta层,添加新素材meta_tiles.png
选择第一个方块,右键
然后选中meta层,将刚才的方块挨个放在之前的障碍物上面,保存地图。
代码中如何判断:
CCPoint tiledCoordForPosition(CCPoint pos)
{
/*函数功能:将像素坐标转换成地图格子坐标*/
CCSize mapTiledNum = m_map->getMapSize();//地图方块数
CCSize tiledSize = m_map->getTiledSize();//单个方块的大小
int x = pos.x/tiledSize.width;
int y = (640-pos.y)/tiledSize.height;
if(x>0){
x-=1;
}
if(y>0){
y-=0;
}
return ccp(x,y);
}
void setTagPosition(int x,int y)
{
/*判断前方是否不可通行*/
/*取主角前方的坐标*/
CCSize spriteSize = GetSprite()->getContentSize();
CCPoint dstPos = CCPoint(x+spriteSize.width/2,y);
/*获得相应坐标的格子位置*/
CCPoint tiledPos = tiledCoordForPosition(ccp(dstPos.x,dstPos.y));
/*获得格子的唯一标示*/
CCTMXLayer* meta = m_map->layerNamed("meta");
meta->setVisible(false);
meta->retain();
int tiledGid = meta->tileGIDAt(tiledPos);
/*不为0表存在这个格子*/
if(tiledGid!=0)
{
/*这个格子既属于meta,同时也属于整个地图的*/
CCDictionary* propertiesDict = m_map->propertiesForGID(tiledGid);
/*取得格子的属性*/
const CCString* prop = propertiesDict->valueForKey("Collidable");
/*判断Collidable里面的属性值是否为true,如果是,不让玩家移动*/
if(prop->m_sString.compare("true")==0){
return;
}
}
Entity::setTagPosition(x,y);
/*以主角为中心移动地图*/
setViewPointByPlayer();
}
4.从障碍层清除当前格子的物体
CCTMXLayer* barrier = m_map->layerNamed("barrier");
barrier->removeTileAt(tiledPos);