Class cc.TMXTiledMap

Class Summary
Constructor Attributes Constructor Name and Description
 
cc.TMXTiledMap(tmxFile, resourcePath)

cc.TMXTiledMap knows how to parse and render a TMX map.

Method Summary

Class Detail

cc.TMXTiledMap(tmxFile, resourcePath)

cc.TMXTiledMap knows how to parse and render a TMX map.

It adds support for the TMX tiled map format used by http://www.mapeditor.org
It supports isometric, hexagonal and orthogonal tiles.
It also supports object groups, objects, and properties.

Features:
- Each tile will be treated as an cc.Sprite
- The sprites are created on demand. They will be created only when you call "layer.getTileAt(position)"
- Each tile can be rotated / moved / scaled / tinted / "opacitied", since each tile is a cc.Sprite
- Tiles can be added/removed in runtime
- The z-order of the tiles can be modified in runtime
- Each tile has an anchorPoint of (0,0)
- The anchorPoint of the TMXTileMap is (0,0)
- The TMX layers will be added as a child
- The TMX layers will be aliased by default
- The tileset image will be loaded using the cc.TextureCache
- Each tile will have a unique tag
- Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z
- Each object group will be treated as an cc.MutableArray
- Object class which will contain all the properties in a dictionary
- Properties can be assigned to the Map, Layer, Object Group, and Object

Limitations:
- It only supports one tileset per layer.
- Embeded images are not supported
- It only supports the XML format (the JSON format is not supported)

Technical description:
Each layer is created using an cc.TMXLayer (subclass of cc.SpriteBatchNode). If you have 5 layers, then 5 cc.TMXLayer will be created,
unless the layer visibility is off. In that case, the layer won't be created at all.
You can obtain the layers (cc.TMXLayer objects) at runtime by:
- map.getChildByTag(tag_number); // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...
- map.getLayer(name_of_the_layer);

Each object group is created using a cc.TMXObjectGroup which is a subclass of cc.MutableArray.
You can obtain the object groups at runtime by:
- map.getObjectGroup(name_of_the_object_group);

Each object is a cc.TMXObject.

Each property is stored as a key-value pair in an cc.MutableDictionary.
You can obtain the properties at runtime by:

map.getProperty(name_of_the_property);
layer.getProperty(name_of_the_property);
objectGroup.getProperty(name_of_the_property);
object.getProperty(name_of_the_property);

//example
1.
//create a TMXTiledMap with file name
var tmxTiledMap = new cc.TMXTiledMap("res/orthogonal-test1.tmx");
2.
//create a TMXTiledMap with content string and resource path
var resources = "res/TileMaps";
var filePath = "res/TileMaps/orthogonal-test1.tmx";
var xmlStr = cc.loader.getRes(filePath);
var tmxTiledMap = new cc.TMXTiledMap(xmlStr, resources);
Parameters:
{String} tmxFile
tmxFile fileName or content string
{String} resourcePath
If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.

Field Detail

{Number} mapHeight
- Height of the map
{Number} mapOrientation
- Map orientation
{Number} mapWidth
- Width of the map
{Array} objectGroups
- Object groups of the map
{Array} properties
- Properties from the map. They can be added using tilemap editors
{Number} tileHeight
- Height of a tile
{Number} tileWidth
- Width of a tile

Method Detail

  • {Array} allLayers()
    Return All layers array.
    Returns:
    {Array}
  • <static> {cc.TMXTiledMap|undefined} cc.TMXTiledMap.create(tmxFile, resourcePath)
    Creates a TMX Tiled Map with a TMX file or content string. Implementation cc.TMXTiledMap
    Parameters:
    {String} tmxFile
    tmxFile fileName or content string
    {String} resourcePath
    If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.
    Deprecated:
    since v3.0 please use new cc.TMXTiledMap(tmxFile,resourcePath) instead.
    Returns:
    {cc.TMXTiledMap|undefined}
  • ctor(tmxFile, resourcePath)
    Creates a TMX Tiled Map with a TMX file or content string.
    Constructor of cc.TMXTiledMap
    Parameters:
    {String} tmxFile
    tmxFile fileName or content string
    {String} resourcePath
    If tmxFile is a file name ,it is not required.If tmxFile is content string ,it is must required.
  • {cc.TMXLayer} getLayer(layerName)
    return the TMXLayer for the specific layer
    Parameters:
    {String} layerName
    Returns:
    {cc.TMXLayer}
  • {Number} getMapOrientation()
    map orientation
    Returns:
    {Number}
  • {cc.Size} getMapSize()
    Gets the map size.
    Returns:
    {cc.Size}
  • {cc.TMXObjectGroup} getObjectGroup(groupName)
    Return the TMXObjectGroup for the specific group
    Parameters:
    {String} groupName
    Returns:
    {cc.TMXObjectGroup}
  • {Array} getObjectGroups()
    object groups
    Returns:
    {Array}
  • {object} getProperties()
    Gets the properties
    Returns:
    {object}
  • {object} getPropertiesForGID(GID)
    Return properties dictionary for tile GID
    Parameters:
    {Number} GID
    Returns:
    {object}
  • {String} getProperty(propertyName)
    Return the value for the specific property name
    Parameters:
    {String} propertyName
    Returns:
    {String}
  • {cc.Size} getTileSize()
    Gets the tile size.
    Returns:
    {cc.Size}
  • {Boolean} initWithTMXFile(tmxFile)
    Initializes the instance of cc.TMXTiledMap with tmxFile
    //example
    var map = new cc.TMXTiledMap()
    map.initWithTMXFile("hello.tmx");
    Parameters:
    {String} tmxFile
    Returns:
    {Boolean} Whether the initialization was successful.
  • {Boolean} initWithXML(tmxString, resourcePath)
    Initializes the instance of cc.TMXTiledMap with tmxString
    Parameters:
    {String} tmxString
    {String} resourcePath
    Returns:
    {Boolean} Whether the initialization was successful.
  • {object} propertiesForGID(GID)
    Return properties dictionary for tile GID
    Parameters:
    {Number} GID
    Returns:
    {object}
  • setMapOrientation(Var)
    map orientation
    Parameters:
    {Number} Var
  • setMapSize(Var)
    Set the map size.
    Parameters:
    {cc.Size} Var
  • setObjectGroups(Var)
    object groups
    Parameters:
    {Array} Var
  • setProperties(Var)
    Set the properties
    Parameters:
    {object} Var
  • setTileSize(Var)
    Set the tile size
    Parameters:
    {cc.Size} Var