在cocos2d-x中,瓦片地图一般是用TileMap方案实现
对于Tiled的使用,本文不做介绍,本文着重介绍下cocos2dx中瓦片地图的使用
首先,我们需要知道几个类:CCTMXTiledMap、CCTMXLayer、CCTMXObjectGroup
CCTMXTiledMap代表一个完整的瓦片地图,它负责地图文件的载入、管理以及呈现。CCTMXLayer代表一个瓦片地图中的图层,可以从图层对象获取图层信息,其隶属于CCTMXTiledMap。CCTMXObjectGroup是地图精灵类,用于代表地图中的精灵组。
对于这三个类的常见函数如下
1.CCTMXTiledMap
create(const char *tmxFile)
createWithXML(const char *tmxString, const char *resourcePath)
getMapSize(void)
getObjectGroups(void)
getProperties(void)
getTileSize(void)
initWithTMXFile(const char *tmxFile)
initWithXML(const char *tmxString, const char *resourcePath)
layerNamed(const char *layerName)
objectGroupNamed(const char *groupName)
propertyNamed(const char *propertyName)
propertiesForGID(int GID)
函数意思比较简单,在此补做过多介绍。当然既然有getter访问器,自然也有setter访问器,在此就不列出了.
2.CCTMXLayer
get/setLayerSize
get/setMapTileSize
get/setTileSet
get/setProperties
releaseMap
tileAt
tileGIDAt
setTileGID
removeTileA:根据横纵坐标索引删除该位置图素
positionAt:根据图素横纵坐标索引获取图素位置
setuptiles::创建设置空像素
propertyNamed:根据属性名称获得属性值
3.CCTMXObjectGroup
get/setPositionOffset
get/setObjects
get/setGroupName
get/setProperties
objectNamed
propertyNamed
在此补充下坐标和图素行列数的转换
indexx=Point.x/map->getTileSize.width;
indexy=map->getMapSIze().height-(Point.y)/map->getTileSize().height
简单来说,就是具体位置除以图素的宽高,不过由于地图中的的行列数是从上到下,而cocos2dx中是从下到上,所以有个小小的转换。