Tile map API for Cocos2d-JS
In order to access tile map, Cocos2d-JS access tile map API, the main classes are: TMXTiledMap, TMXLayer and TMXObjectGroup.
1. TMXTiledMap
TMXTiledMap is a tile map class. Its class chart is shown in. TMXTiledMap is derived from the Node class and has Node features.
Common functions of TMXTiledMap are as follows:
New cc. TMXTiledMap (tmxFile ). Create a tile map object.
GetLayer (layerName ). Obtain the layer object by using the layer name.
GetObjectGroup (groupName ). Obtain the object group set in the layer by using the Object layer name.
GetObjectGroups (). Obtains the set of all object groups in the Object layer.
GetProperties (). Obtain all attributes in the layer.
GetPropertiesForGID (GID ). GID [GID is the global identifier of a tile.] Obtain attributes.
GetMapSize (). Obtain the map size, which is measured in tiles.
GetTileSize (). Obtain the tile size in pixels.
The sample code is as follows:
Var group = _ tileMap. getObjectGroup (Objects );
Var background = _ tileMap. getLayer (Background );
_ TileMap is a tile map object.
2. TMXLayer
TMXLayer is a map layer class. Its Class diagram is shown in. TMXLayer is also derived from the Node class and has Node features. At the same time, TMXLayer is also derived from the SpriteBatchNode class. All TMXLayer objects have batch rendering capabilities. The tile map layer is composed of a large number of repeated images, and they need to be rendered to improve performance.
Common functions of TMXLayer are as follows:
GetLayerName (). Obtain the layer name.
GetLayerSize (). Obtain the layer size, which is measured in tiles.
GetMapTileSize (). Obtain the tile size in pixels.
GetPositionAt (pos ). Pixel coordinates are obtained through tile coordinates. the y-axis direction of tile coordinates is opposite to that of the Y-axis of pixel coordinates.
GetTileGIDAt (pos ). Obtain the GID value through the tile coordinate.
3. TMXObjectGroup
TMXObjectGroup is a collection of object groups in the object Layer. Its Class diagram is shown in. Note that TMXObjectGroup is different from TMXLayer. TMXObjectGroup is not derived from Node and does not have the Node feature.
Common functions of TMXObjectGroup are as follows:
PropertyNamed (propertyName ). Obtain the attribute value through the attribute name.
ObjectNamed (objectName ). Obtain object information by Object Name.
GetProperties (). Obtain the attributes of an object.
GetObjects (). Obtain all objects.