cocos2dx框架自帶的地圖CCTMXTiledMap,繼承自CCNode。CCTMXTiledMap的座標系的原點位於左上角,以一個瓦片爲單位,換句話說,左上角第一塊瓦片的座標爲(0,0),而緊挨着它的右邊的瓦片座標就是(1,0)。TileMap中的每個瓦片擁有一個惟一的編號GID,用於在地圖中查找某個瓦片。Cocos2d-x提供了一系列方法,能夠從瓦片地圖座標獲取對應瓦片的GID,同時還能夠利用這些方法來判斷某個座標下是否存在瓦片。node
屬性:app
CCSize m_tMapSize,地圖大小(以瓦片爲單位)框架
CCSize m_tTileSize,瓦片大小(以像素爲單位)less
int m_nMapOrientation,地圖類型(enum{CCTMXOrientationOrtho,CCTMXOrientationHex,CCTMXOrientationIso,};)ide
CCArray* m_pObjectGroups,對象集合優化
CCDictionary* m_pProperties,屬性字典ui
CCDictionary* m_pTileProperties,瓦片屬性this
方法:lua
建立地圖spa
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
1 #ifndef __CCTMX_LAYER_H__ 2 #define __CCTMX_LAYER_H__ 3 4 #include "CCTMXObjectGroup.h" 5 #include "base_nodes/CCAtlasNode.h" 6 #include "sprite_nodes/CCSpriteBatchNode.h" 7 #include "CCTMXXMLParser.h" 8 NS_CC_BEGIN 9 10 class CCTMXMapInfo; 11 class CCTMXLayerInfo; 12 class CCTMXTilesetInfo; 13 struct _ccCArray; 14 15 class CC_DLL CCTMXLayer : public CCSpriteBatchNode 16 { 17 /** layer尺寸( 以tile爲單位) */ 18 CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tLayerSize, LayerSize); 19 /** size of the map's tile (could be different from the tile's size) */ 20 /** layer尺寸( 以tile爲單位,可能與tile不一樣) */ 21 CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tMapTileSize, MapTileSize); 22 /** pointer to the map of tiles */ 23 CC_SYNTHESIZE(unsigned int*, m_pTiles, Tiles); 24 /** Tileset information for the layer */ 25 CC_PROPERTY(CCTMXTilesetInfo*, m_pTileSet, TileSet); 26 /** Layer orientation, which is the same as the map orientation */ 27 CC_SYNTHESIZE(unsigned int, m_uLayerOrientation, LayerOrientation); 28 /** properties from the layer. They can be added using Tiled */ 29 CC_PROPERTY(CCDictionary*, m_pProperties, Properties); 30 public: 31 /** 32 * @js ctor 33 * @lua NA 34 */ 35 CCTMXLayer(); 36 /** 37 * @js NA 38 * @lua NA 39 */ 40 virtual ~CCTMXLayer(); 41 42 /** creates a CCTMXLayer with an tileset info, a layer info and a map info */ 43 static CCTMXLayer * create(CCTMXTilesetInfo *tilesetInfo, CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo); 44 45 /** initializes a CCTMXLayer with a tileset info, a layer info and a map info 46 * @lua NA 47 */ 48 bool initWithTilesetInfo(CCTMXTilesetInfo *tilesetInfo, CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo); 49 50 /** dealloc the map that contains the tile position from memory. 51 Unless you want to know at runtime the tiles positions, you can safely call this method. 52 If you are going to call layer->tileGIDAt() then, don't release the map 53 */ 54 void releaseMap(); 55 56 /** returns the tile (CCSprite) at a given a tile coordinate. 57 The returned CCSprite will be already added to the CCTMXLayer. Don't add it again. 58 The CCSprite can be treated like any other CCSprite: rotated, scaled, translated, opacity, color, etc. 59 You can remove either by calling: 60 - layer->removeChild(sprite, cleanup); 61 - or layer->removeTileAt(ccp(x,y)); 62 @js getTileGIDAt 63 */ 64 CCSprite* tileAt(const CCPoint& tileCoordinate); 65 66 /** returns the tile gid at a given tile coordinate. 67 if it returns 0, it means that the tile is empty. 68 This method requires the the tile map has not been previously released (eg. don't call layer->releaseMap()) 69 @js tileGIDAt 70 */ 71 unsigned int tileGIDAt(const CCPoint& tileCoordinate); 72 73 /** returns the tile gid at a given tile coordinate. It also returns the tile flags. 74 This method requires the the tile map has not been previously released (eg. don't call [layer releaseMap]) 75 @js tileGIDAt 76 @lua NA 77 */ 78 unsigned int tileGIDAt(const CCPoint& tileCoordinate, ccTMXTileFlags* flags); 79 80 /** sets the tile gid (gid = tile global id) at a given tile coordinate. 81 The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1. 82 If a tile is already placed at that position, then it will be removed. 83 */ 84 void setTileGID(unsigned int gid, const CCPoint& tileCoordinate); 85 86 /** sets the tile gid (gid = tile global id) at a given tile coordinate. 87 The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1. 88 If a tile is already placed at that position, then it will be removed. 89 90 Use withFlags if the tile flags need to be changed as well 91 */ 92 93 void setTileGID(unsigned int gid, const CCPoint& tileCoordinate, ccTMXTileFlags flags); 94 95 /** removes a tile at given tile coordinate */ 96 void removeTileAt(const CCPoint& tileCoordinate); 97 98 /** returns the position in points of a given tile coordinate 99 * @js getPositionAt 100 */ 101 CCPoint positionAt(const CCPoint& tileCoordinate); 102 103 /** return the value for the specific property name 104 * @js getProperty 105 */ 106 CCString *propertyNamed(const char *propertyName); 107 108 /** Creates the tiles */ 109 void setupTiles(); 110 111 /** CCTMXLayer doesn't support adding a CCSprite manually. 112 * @warning addchild(z, tag); is not supported on CCTMXLayer. Instead of setTileGID. 113 * @lua NA 114 */ 115 virtual void addChild(CCNode * child, int zOrder, int tag); 116 /** super method 117 * @lua NA 118 */ 119 void removeChild(CCNode* child, bool cleanup); 120 121 inline const char* getLayerName(){ return m_sLayerName.c_str(); } 122 inline void setLayerName(const char *layerName){ m_sLayerName = layerName; } 123 private: 124 CCPoint positionForIsoAt(const CCPoint& pos); 125 CCPoint positionForOrthoAt(const CCPoint& pos); 126 CCPoint positionForHexAt(const CCPoint& pos); 127 128 CCPoint calculateLayerOffset(const CCPoint& offset); 129 130 /* optimization methods */ 131 CCSprite* appendTileForGID(unsigned int gid, const CCPoint& pos); 132 CCSprite* insertTileForGID(unsigned int gid, const CCPoint& pos); 133 CCSprite* updateTileForGID(unsigned int gid, const CCPoint& pos); 134 135 /* The layer recognizes some special properties, like cc_vertez */ 136 void parseInternalProperties(); 137 void setupTileSprite(CCSprite* sprite, CCPoint pos, unsigned int gid); 138 CCSprite* reusedTileWithRect(CCRect rect); 139 int vertexZForPos(const CCPoint& pos); 140 141 // index 142 unsigned int atlasIndexForExistantZ(unsigned int z); 143 unsigned int atlasIndexForNewZ(int z); 144 protected: 145 // layer的名稱 146 std::string m_sLayerName; 147 //TMX Layer 的透明度 148 unsigned char m_cOpacity; 149 150 unsigned int m_uMinGID; 151 unsigned int m_uMaxGID; 152 153 //vertexZ 啓用後該參數纔有效 154 int m_nVertexZvalue; 155 bool m_bUseAutomaticVertexZ; 156 157 // 用於優化 158 CCSprite *m_pReusedTile; 159 ccCArray *m_pAtlasIndexArray; 160 161 // 用於視網膜顯示屏 162 float m_fContentScaleFactor; 163 }; 164 165 166 NS_CC_END 167 168 #endif //__CCTMX_LAYER_H__