cocos2dx框架自带的地图CCTMXTiledMap,继承自CCNode。CCTMXTiledMap的坐标系的原点位于左上角,以一个瓦片为单位,换句话说,左上角第一块瓦片的坐标为(0,0),而紧挨着它的右边的瓦片坐标就是(1,0)。TileMap中的每一个瓦片拥有一个唯一的编号GID,用于在地图中查找某个瓦片。Cocos2d-x提供了一系列方法,可以从瓦片地图坐标获取对应瓦片的GID,同时还可以利用这些方法来判断某个坐标下是否存在瓦片。
属性:
CCSize m_tMapSize,地图大小(以瓦片为单位)
CCSize m_tTileSize,瓦片大小(以像素为单位)
int m_nMapOrientation,地图类型(enum{CCTMXOrientationOrtho,CCTMXOrientationHex,)
CCArray* m_pObjectGroups,对象集合
CCDictionary* m_pProperties,属性字典
CCDictionary* m_pTileProperties,瓦片属性
方法:
创建地图
static CCTMXTiledMap* create(const char *tmxFile)
static CCTMXTiledMap* createWithXML(const char* tmxString, const char* resourcePath)
地图初始化
bool initWithTMXFile(const char *tmxFile)
bool initWithXML(const char* tmxString, const char* resourcePath)
获取地图层
CCTMXLayer* layerNamed(const char *layerName)
获取根据名称对象组
CCTMXObjectGroup* objectGroupNamed(const char *groupName)
获取属性值
CCString *propertyNamed(const char *propertyName)
根据瓦片GID获取属性字典
CCDictionary* propertiesForGID(int GID)
CCTMXLayer继承自CCSpriteBatchNode,代表一个瓦片地图中的图层,可以从图层对象获取图层信息,如某一点是否存在对象组或属性。CCTMXLayer坐标以地图层的左下角为原点(不是屏幕左下角),以像素为单位,当把地图层坐标pos转换为世界坐标时需要将pos的x坐标减去地图滚动的距离。当把CCTMXTiledMap坐标转换为CCTMXLayer坐标时,注意乘瓦片大小和原点转换。
属性:
std::string m_sLayerName,图层名称
unsigned char m_cOpacity,透明度
unsigned int m_uMinGID
unsigned int m_uMaxGID
int m_nVertexZvalue,Z轴
bool m_bUseAutomaticVertexZ,由框架自动管理Z轴值
float m_fContentScaleFactor,缩放
CCSize m_tLayerSize,图层大小(以瓦片为单位)
CCSize m_tMapTileSize,瓦片大小(可能与tileMap不同)
unsigned int* m_pTiles,指向瓦片的指针
CCTMXTilesetInfo* m_pTileSet,图层瓦片信息
unsigned int m_uLayerOrientation,和tileMap一样
CCDictionary* m_pProperties图层属性
方法
CCSprite* tileAt(const CCPoint& tileCoordinate),根据瓦片坐标返回瓦片精灵
unsigned int tileGIDAt(const CCPoint& tileCoordinate),根据瓦片坐标返回GID,如为空则返回0
unsigned int tileGIDAt(const CCPoint& tileCoordinate, ccTMXTileFlags* flags),同上且返回flags
void setTileGID(unsigned int gid, const CCPoint& tileCoordinate),设置GID
void setTileGID(unsigned int gid, const CCPoint& tileCoordinate, ccTMXTileFlags flags)
void removeTileAt(const CCPoint& tileCoordinate),删除瓦片
CCPoint positionAt(const CCPoint& tileCoordinate),返回像素坐标
CCString *propertyNamed(const char *propertyName),获取属性
virtual void addChild(CCNode * child, int zOrder, int tag),添加对象
void removeChild(CCNode* child, bool cleanup),删除对象
const char* getLayerName()
void setLayerName(const char *layerName)
1 #ifndef __CCTMX_TILE_MAP_H__ 2 #define __CCTMX_TILE_MAP_H__ 3 4 #include "base_nodes/CCNode.h" 5 #include "CCTMXObjectGroup.h" 6 7 NS_CC_BEGIN 8 9 class CCTMXObjectGroup; 10 class CCTMXLayer; 11 class CCTMXLayerInfo; 12 class CCTMXTilesetInfo; 13 class CCTMXMapInfo; 14 15 /** TMX地图类型 */ 16 enum 17 { 18 /** 平面地图 */ 19 CCTMXOrientationOrtho, 20 21 /** 六角地图 */ 22 CCTMXOrientationHex, 23 24 /** 三维地图(45度斜视) */ 25 CCTMXOrientationIso, 26 }; 27 28 class CC_DLL CCTMXTiledMap : public CCNode 29 { 30 /** 地图尺寸(以瓦片为单位) */ 31 CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tMapSize, MapSize); 32 33 /** 地图尺寸(以像素为单位) */ 34 CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tTileSize, TileSize); 35 36 /** 地图类型(朝向) */ 37 CC_SYNTHESIZE(int, m_nMapOrientation, MapOrientation); 38 39 /** 地图内对象集合 */ 40 CC_PROPERTY(CCArray*, m_pObjectGroups, ObjectGroups); 41 42 /** 地图内对象字典 */ 43 CC_PROPERTY(CCDictionary*, m_pProperties, Properties); 44 public: 45 46 CCTMXTiledMap(); 47 48 virtual ~CCTMXTiledMap(); 49 50 51 /** 根据tmx文件创建地图 */ 52 static CCTMXTiledMap* create(const char *tmxFile); 53 54 /** 根据xml字符串和资源路径创建地图 */ 55 static CCTMXTiledMap* createWithXML(const char* tmxString, const char* resourcePath); 56 57 58 /** 根据tmx文件初始化地图 */ 59 bool initWithTMXFile(const char *tmxFile); 60 61 /** 根据xml字符串和资源路径初始化地图 */ 62 bool initWithXML(const char* tmxString, const char* resourcePath); 63 64 65 /** 获取地图中的层 */ 66 CCTMXLayer* layerNamed(const char *layerName); 67 68 69 /** 获取对象集合 */ 70 CCTMXObjectGroup* objectGroupNamed(const char *groupName); 71 72 73 /** 获取属性值 */ 74 CCString *propertyNamed(const char *propertyName); 75 76 77 /** 根据GID返回属性字典 */ 78 CCDictionary* propertiesForGID(int GID); 79 80 private: 81 CCTMXLayer * parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo); 82 CCTMXTilesetInfo * tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo); 83 void buildWithMapInfo(CCTMXMapInfo* mapInfo); 84 protected: 85 //地图属性字典 86 CCDictionary* m_pTileProperties; 87 88 }; 89 90 NS_CC_END