一、TMXTiledMap類
cocos2dx提供了TMXTiledMap類來管理tiled瓦片地圖,主要包括地圖層、瓦片信息等,其主要方法、屬性如下:
class CC_DLL TMXTiledMap : public Node
{
// 使用.tmx格式的文件創建瓦片地圖
static TMXTiledMap* create(const std::string& tmxFile);
/**
* 獲取瓦片地圖的屬性信息
**/
// 獲取瓦片地圖的指定名稱的屬性值
Value getProperty(const std::string& propertyName) const;
// 獲取瓦片地圖的所有屬性(鍵-值對)
void setProperties(const ValueMap& properties); // 可以修改屬性
ValueMap& getProperties();
// 獲取瓦片地圖(單位:瓦片數量)
void setMapSize(const Size& mapSize);
Size& getMapSize() const;
// 獲取單個瓦片的尺寸(單位:像素)
void setTileSize(const Size& tileSize);
Size& getTileSize() const;
// 通過GID獲取圖塊的屬性,返回Value字典
// 其實返回的是:ValueMap,即(鍵-值對)
Value getPropertiesForGID(int GID) const;
/**
* 獲取地圖層、對象層
**/
// 獲取指定名稱的地圖層 TMXLayer
TMXLayer* getLayer(const std::string& layerName) const;
// 獲取指定名稱的對象層 TMXObjectGroup
TMXObjectGroup* getObjectGroup(const std::string& groupName) const;
// 獲取瓦片地圖的所有對象層,返回對象數組 Vector<TMXObjectGroup*>
void setObjectGroups(const Vector<TMXObjectGroup*>& groups);
Vector<TMXObjectGroup*>& getObjectGroups() const;
// 瓦片地圖的尺寸,以瓦片數量爲單位
Size _mapSize;
// 瓦片的尺寸,以瓦片像素爲單位
Size _tileSize;
};
二、TMXLayer類
TMXLayer類管理tiled的地圖層,包含該地圖層中的每個瓦片的信息,而每一個瓦片都被表示成精靈,其主要方法、屬性如下:
class CC_DLL TMXLayer : public SpriteBatchNode
{
/**
* 獲取地圖層的屬性信息
**/
// 獲取地圖層的名字
void setLayerName(const std::string& layerName); // 可以重新設置地圖層名字
std::string& getLayerName();
// 獲取地圖層的propertyName屬性值
Value getProperty(const std::string& propertyName) const;
// 獲取地圖層的所有自定義屬性字典(鍵-值對)
void setProperties(const ValueMap& properties);
ValueMap& getProperties();
// 獲取地圖層尺寸,一般等於瓦片地圖的尺寸(單位:瓦片數量)
void setLayerSize(const Size& size);
Size& getLayerSize() const;
// 設置瓦片尺寸的大小,一般與瓦片地圖的瓦片尺寸是一樣的(單位:像素)
void setMapTileSize(const Size& size);
Size& getMapTileSize() const;
/**
* 對地圖層的瓦片進行操作
**/
// 獲取指定tile座標的瓦片(Sprite)
Sprite* getTileAt(const Vec2& tileCoordinate);
// 可通過調用如下方法對瓦片精靈進行刪除:
void removeTileAt(const Vec2& tileCoordinate);
void removeChild(Node* child, bool cleanup) override;
// 獲取指定tile座標的瓦片對應的OpenGL座標位置
Vec2 getPositionAt(const Vec2& tileCoordinate);
// 設置指定tile座標的瓦片,將其圖片變爲GID的圖塊。
void setTileGID(uint32_t gid, const Vec2& tileCoordinate);
// 獲取指定tile座標的瓦片所使用的圖塊的GID。
uint32_t getTileGIDAt(const Vec2& tileCoordinate);
// 地圖層的名字
std::string _layerName;
// 地圖層的尺寸,以瓦片數量爲單位
Size _layerSize;
// 地圖層的瓦片的尺寸,以瓦片像素爲單位
Size _mapTileSize;
};
三、TMXObjectGroup類
TMXObjectGroup類用於管理tiled地圖中特殊的地圖層,即對象層,包括所有的對象及其所有的屬性,而每一個對象的所有屬性被存儲爲ValueMap鍵-值對映射,其主要方法、屬性如下:
class CC_DLL TMXObjectGroup : public Ref
{
/**
* 獲取對象層的屬性信息
**/
// 獲取對象層的名稱
void setGroupName(const std::string& groupName); // 可以重新設置對象層名稱
std::string& getGroupName();
// 獲取對象層的propertyName屬性值
Value getProperty(const std::string& propertyName) const;
// 獲取對象層所有屬性(鍵-值對)
void setProperties(const ValueMap& properties);
ValueMap& getProperties();
// 獲取對象層指定的objectName對象,其所有屬性被存儲爲ValueMap(鍵-值對)
ValueMap getObject(const std::string& objectName) const;
// 獲取對象層的所有對象
void setObjects(const ValueVector& objects);
ValueVector& getObjects();
// 對象層的名稱
std::string _groupName;
// 所有的對象
ValueVector _objects;
};