new Tilemap(game, key)
A Tile Map object. A Tile map consists of a set of tile data and tile sets. It is rendered to the display using a TilemapLayer. A map may have multiple layers. You can perform operations on the map data such as copying, pasting, filling and shuffling the tiles around.
Parameters:
Name | Type | Argument | Description |
---|---|---|---|
game |
Phaser.Game | Game reference to the currently running game. |
|
key |
string |
<optional> |
The key of the tilemap data as stored in the Cache. |
- Source:
Members
-
<static, constant> CSV :number
-
Type:
- number
- Source:
-
<static, constant> TILED_JSON :number
-
Type:
- number
- Source:
-
collision
-
- Source:
Properties:
Name Type Description collision
array An array of collision data (polylines, etc).
-
currentLayer
-
- Source:
Properties:
Name Type Description currentLayer
number The current layer.
-
debugMap
-
- Source:
Properties:
Name Type Description debugMap
array Map data used for debug values only.
-
game
-
- Source:
Properties:
Name Type Description game
Phaser.Game A reference to the currently running Game.
-
height
-
- Source:
Properties:
Name Type Description height
number The height of the map (in tiles).
-
heightInPixels
-
- Source:
Properties:
Name Type Description heightInPixels
number The height of the map in pixels based on height * tileHeight.
-
images
-
- Source:
Properties:
Name Type Description images
array An array of Tiled Image Layers.
-
key
-
- Source:
Properties:
Name Type Description key
string The key of this map data in the Phaser.Cache.
-
layers
-
- Source:
Properties:
Name Type Description layers
array An array of Tilemap layer data.
-
objects
-
- Source:
Properties:
Name Type Description objects
array An array of Tiled Object Layers.
-
orientation
-
- Source:
Properties:
Name Type Description orientation
string The orientation of the map data (as specified in Tiled), usually 'orthogonal'.
-
properties
-
- Source:
Properties:
Name Type Description properties
object Map specific properties as specified in Tiled.
-
tileHeight
-
- Source:
Properties:
Name Type Description tileHeight
number The base height of the tiles in the map (in pixels).
-
tiles
-
- Source:
Properties:
Name Type Description tiles
array The super array of Tiles.
-
tilesets
-
- Source:
Properties:
Name Type Description tilesets
array An array of Tilesets.
-
tileWidth
-
- Source:
Properties:
Name Type Description tileWidth
number The base width of the tiles in the map (in pixels).
-
version
-
- Source:
Properties:
Name Type Description version
number The version of the map data (as specified in Tiled, usually 1).
-
width
-
- Source:
Properties:
Name Type Description width
number The width of the map (in tiles).
-
widthInPixels
-
- Source:
Properties:
Name Type Description widthInPixels
number The width of the map in pixels based on width * tileWidth.
Methods
-
addTilesetImage(tileset, key)
-
Adds an image to the map to be used as a tileset. A single map may use multiple tilesets. Note that the tileset name can be found in the JSON file exported from Tiled, or in the Tiled editor.
Parameters:
Name Type Argument Description tileset
string The name of the tileset as specified in the map data.
key
string <optional>
The key of the Phaser.Cache image used for this tileset. If not specified it will look for an image with a key matching the tileset parameter.
- Source:
-
<protected> calculateFaces(layer)
-
Internal function.
Parameters:
Name Type Description layer
number The index of the TilemapLayer to operate on.
- Source:
-
clearPhysicsBodies(layer)
-
Clears all physics bodies from the world for the given layer.
Parameters:
Name Type Argument Description layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on. If not given will default to this.currentLayer.
- Source:
-
copy(x, y, width, height, layer) → {array}
-
Copies all of the tiles in the given rectangular block into the tilemap data buffer.
Parameters:
Name Type Argument Description x
number X position of the top left of the area to copy (given in tiles, not pixels)
y
number Y position of the top left of the area to copy (given in tiles, not pixels)
width
number The width of the area to copy (given in tiles, not pixels)
height
number The height of the area to copy (given in tiles, not pixels)
layer
number | string | Phaser.TilemapLayer <optional>
The layer to copy the tiles from.
- Source:
Returns:
An array of the tiles that were copied.
- Type
- array
-
create(name, width, height)
-
Creates an empty map of the given dimensions.
Parameters:
Name Type Description name
string The name of the map (mostly used for debugging)
width
number The width of the map in tiles.
height
number The height of the map in tiles.
- Source:
-
createCollisionObjects(layer, addToWorld) → {array}
-
Converts all of the polylines inside a Tiled ObjectGroup into physics bodies that are added to the world. Note that the polylines must be created in such a way that they can withstand polygon decomposition.
Parameters:
Name Type Argument Default Description layer
string <optional>
The Tiled layer to operate on that contains the collision data.
addToWorld
boolean <optional>
true If true it will automatically add each body to the world.
- Source:
Returns:
An array of the Phaser.Physics.Body objects that have been created.
- Type
- array
-
createFromObjects(name, gid, key, frame, exists, autoCull, group)
-
Creates a Sprite for every object matching the given gid in the map data. You can optionally specify the group that the Sprite will be created in. If none is given it will be created in the World. All properties from the map data objectgroup are copied across to the Sprite, so you can use this as an easy way to configure Sprite properties from within the map editor. For example giving an object a property if alpha: 0.5 in the map editor will duplicate that when the Sprite is created. You could also give it a value like: body.velocity.x: 100 to set it moving automatically.
Parameters:
Name Type Argument Default Description name
string The name of the Object Group to create Sprites from.
gid
number The layer array index value, or if a string is given the layer name, within the map data that this TilemapLayer represents.
key
string The Game.cache key of the image that this Sprite will use.
frame
number | string <optional>
If the Sprite image contains multiple frames you can specify which one to use here.
exists
boolean <optional>
true The default exists state of the Sprite.
autoCull
boolean <optional>
true The default autoCull state of the Sprite. Sprites that are autoCulled are culled from the camera if out of its range.
group
Phaser.Group <optional>
Optional Group to add the Sprite to. If not specified it will be added to the World group.
- Source:
-
createLayer(layer, width, height, group) → {Phaser.TilemapLayer}
-
Creates a new TilemapLayer object. By default TilemapLayers are fixed to the camera. The
layer
parameter is important. If you've created your map in Tiled then you can get this by looking in Tiled and looking at the Layer name. Or you can open the JSON file it exports and look at the layers[].name value. Either way it must match.Parameters:
Name Type Argument Description layer
number | string The layer array index value, or if a string is given the layer name, within the map data that this TilemapLayer represents.
width
number <optional>
The rendered width of the layer, should never be wider than Game.width. If not given it will be set to Game.width.
height
number <optional>
The rendered height of the layer, should never be wider than Game.height. If not given it will be set to Game.height.
group
Phaser.Group <optional>
Optional Group to add the object to. If not specified it will be added to the World group.
- Source:
Returns:
The TilemapLayer object. This is an extension of Phaser.Sprite and can be moved around the display list accordingly.
- Type
- Phaser.TilemapLayer
-
destroy()
-
Removes all layers from this tile map and nulls the game reference.
- Source:
-
dump()
-
Dumps the tilemap data out to the console.
- Source:
-
fill(index, x, y, width, height, layer)
-
Fills the given area with the specified tile.
Parameters:
Name Type Argument Description index
number The index of the tile that the area will be filled with.
x
number X position of the top left of the area to operate one, given in tiles, not pixels.
y
number Y position of the top left of the area to operate one, given in tiles, not pixels.
width
number The width in tiles of the area to operate on.
height
number The height in tiles of the area to operate on.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on.
- Source:
-
forEach(callback, context, x, y, width, height, layer)
-
For each tile in the given area defined by x/y and width/height run the given callback.
Parameters:
Name Type Argument Description callback
number The callback. Each tile in the given area will be passed to this callback as the first and only parameter.
context
number The context under which the callback should be run.
x
number X position of the top left of the area to operate one, given in tiles, not pixels.
y
number Y position of the top left of the area to operate one, given in tiles, not pixels.
width
number The width in tiles of the area to operate on.
height
number The height in tiles of the area to operate on.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on.
- Source:
-
generateCollisionData(layer, addToWorld) → {array}
-
Goes through all tiles in the given layer and converts those set to collide into physics bodies in the world. Only call this after you have specified all of the tiles you wish to collide with calls like Tilemap.setCollisionBetween, etc. Every time you call this method it will destroy any previously created bodies and remove them from the world. Therefore understand it's an expensive operation and not to be done in a core game update loop.
Parameters:
Name Type Argument Default Description layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on. If not given will default to this.currentLayer.
addToWorld
boolean <optional>
true If true it will automatically add each body to the world, otherwise it's up to you to do so.
- Source:
Returns:
An array of the Phaser.Physics.Body objects that have been created.
- Type
- array
-
getImageIndex(name) → {number}
-
Gets the image index based on its name.
Parameters:
Name Type Description name
string The name of the image to get.
- Source:
Returns:
The index of the image in this tilemap, or null if not found.
- Type
- number
-
<protected> getIndex(location, name) → {number}
-
Gets the layer index based on the layers name.
Parameters:
Name Type Description location
array The local array to search.
name
string The name of the array element to get.
- Source:
Returns:
The index of the element in the array, or null if not found.
- Type
- number
-
<protected> getLayer(layer) → {number}
-
Gets the TilemapLayer index as used in the setCollision calls.
Parameters:
Name Type Description layer
number | string | Phaser.TilemapLayer The layer to operate on. If not given will default to this.currentLayer.
- Source:
Returns:
The TilemapLayer index.
- Type
- number
-
getLayerIndex(name) → {number}
-
Gets the layer index based on its name.
Parameters:
Name Type Description name
string The name of the layer to get.
- Source:
Returns:
The index of the layer in this tilemap, or null if not found.
- Type
- number
-
getObjectIndex(name) → {number}
-
Gets the object index based on its name.
Parameters:
Name Type Description name
string The name of the object to get.
- Source:
Returns:
The index of the object in this tilemap, or null if not found.
- Type
- number
-
getTile(x, y, layer) → {Phaser.Tile}
-
Gets a tile from the Tilemap Layer. The coordinates are given in tile values.
Parameters:
Name Type Argument Description x
number X position to get the tile from (given in tile units, not pixels)
y
number Y position to get the tile from (given in tile units, not pixels)
layer
number | string | Phaser.TilemapLayer <optional>
The layer to get the tile from.
- Source:
Returns:
The tile at the given coordinates.
- Type
- Phaser.Tile
-
getTileAbove(layer, x, y)
-
Gets the tile above the tile coordinates given. Mostly used as an internal function by calculateFaces.
Parameters:
Name Type Description layer
number The local layer index to get the tile from. Can be determined by Tilemap.getLayer().
x
number The x coordinate to get the tile from. In tiles, not pixels.
y
number The y coordinate to get the tile from. In tiles, not pixels.
- Source:
-
getTileBelow(layer, x, y)
-
Gets the tile below the tile coordinates given. Mostly used as an internal function by calculateFaces.
Parameters:
Name Type Description layer
number The local layer index to get the tile from. Can be determined by Tilemap.getLayer().
x
number The x coordinate to get the tile from. In tiles, not pixels.
y
number The y coordinate to get the tile from. In tiles, not pixels.
- Source:
-
getTileLeft(layer, x, y)
-
Gets the tile to the left of the tile coordinates given. Mostly used as an internal function by calculateFaces.
Parameters:
Name Type Description layer
number The local layer index to get the tile from. Can be determined by Tilemap.getLayer().
x
number The x coordinate to get the tile from. In tiles, not pixels.
y
number The y coordinate to get the tile from. In tiles, not pixels.
- Source:
-
getTileRight(layer, x, y)
-
Gets the tile to the right of the tile coordinates given. Mostly used as an internal function by calculateFaces.
Parameters:
Name Type Description layer
number The local layer index to get the tile from. Can be determined by Tilemap.getLayer().
x
number The x coordinate to get the tile from. In tiles, not pixels.
y
number The y coordinate to get the tile from. In tiles, not pixels.
- Source:
-
getTilesetIndex(name) → {number}
-
Gets the tileset index based on its name.
Parameters:
Name Type Description name
string The name of the tileset to get.
- Source:
Returns:
The index of the tileset in this tilemap, or null if not found.
- Type
- number
-
getTileWorldXY(x, y, layer) → {Phaser.Tile}
-
Gets a tile from the Tilemap layer. The coordinates are given in pixel values.
Parameters:
Name Type Argument Description x
number X position to get the tile from (given in pixels)
y
number Y position to get the tile from (given in pixels)
layer
number | string | Phaser.TilemapLayer <optional>
The layer to get the tile from.
- Source:
Returns:
The tile at the given coordinates.
- Type
- Phaser.Tile
-
paste(x, y, tileblock, layer)
-
Pastes a previously copied block of tile data into the given x/y coordinates. Data should have been prepared with Tilemap.copy.
Parameters:
Name Type Argument Description x
number X position of the top left of the area to paste to (given in tiles, not pixels)
y
number Y position of the top left of the area to paste to (given in tiles, not pixels)
tileblock
array The block of tiles to paste.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to paste the tiles into.
- Source:
-
putTile(tile, x, y, layer)
-
Puts a tile of the given index value at the coordinate specified.
Parameters:
Name Type Argument Description tile
Phaser.Tile | number The index of this tile to set or a Phaser.Tile object.
x
number X position to place the tile (given in tile units, not pixels)
y
number Y position to place the tile (given in tile units, not pixels)
layer
number | string | Phaser.TilemapLayer <optional>
The layer to modify.
- Source:
-
putTileWorldXY(tile, x, y, tileWidth, tileHeight, layer)
-
Puts a tile into the Tilemap layer. The coordinates are given in pixel values.
Parameters:
Name Type Argument Description tile
Phaser.Tile | number The index of this tile to set or a Phaser.Tile object.
x
number X position to insert the tile (given in pixels)
y
number Y position to insert the tile (given in pixels)
tileWidth
number The width of the tile in pixels.
tileHeight
number The height of the tile in pixels.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to modify.
- Source:
-
random(x, y, width, height, layer)
-
Randomises a set of tiles in a given area.
Parameters:
Name Type Argument Description x
number X position of the top left of the area to operate one, given in tiles, not pixels.
y
number Y position of the top left of the area to operate one, given in tiles, not pixels.
width
number The width in tiles of the area to operate on.
height
number The height in tiles of the area to operate on.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on.
- Source:
-
removeAllLayers()
-
Removes all layers from this tile map.
- Source:
-
replace(source, dest, x, y, width, height, layer)
-
Scans the given area for tiles with an index matching
source
and updates their index to matchdest
.Parameters:
Name Type Argument Description source
number The tile index value to scan for.
dest
number The tile index value to replace found tiles with.
x
number X position of the top left of the area to operate one, given in tiles, not pixels.
y
number Y position of the top left of the area to operate one, given in tiles, not pixels.
width
number The width in tiles of the area to operate on.
height
number The height in tiles of the area to operate on.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on.
- Source:
-
setCollision(indexes, collides, layer)
-
Sets collision the given tile or tiles. You can pass in either a single numeric index or an array of indexes: [ 2, 3, 15, 20]. The
collides
parameter controls if collision will be enabled (true) or disabled (false).Parameters:
Name Type Argument Default Description indexes
number | array Either a single tile index, or an array of tile IDs to be checked for collision.
collides
boolean <optional>
true If true it will enable collision. If false it will clear collision.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on. If not given will default to this.currentLayer.
- Source:
-
setCollisionBetween(start, stop, collides, layer)
-
Sets collision on a range of tiles where the tile IDs increment sequentially. Calling this with a start value of 10 and a stop value of 14 would set collision for tiles 10, 11, 12, 13 and 14. The
collides
parameter controls if collision will be enabled (true) or disabled (false).Parameters:
Name Type Argument Default Description start
number The first index of the tile to be set for collision.
stop
number The last index of the tile to be set for collision.
collides
boolean <optional>
true If true it will enable collision. If false it will clear collision.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on. If not given will default to this.currentLayer.
- Source:
-
setCollisionByExclusion(indexes, collides, layer)
-
Sets collision on all tiles in the given layer, except for the IDs of those in the given array. The
collides
parameter controls if collision will be enabled (true) or disabled (false).Parameters:
Name Type Argument Default Description indexes
array An array of the tile IDs to not be counted for collision.
collides
boolean <optional>
true If true it will enable collision. If false it will clear collision.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on. If not given will default to this.currentLayer.
- Source:
-
<protected> setCollisionByIndex(index, collides, layer, recalculate)
-
Sets collision values on a tile in the set. You shouldn't usually call this method directly, instead use setCollision, setCollisionBetween or setCollisionByExclusion.
Parameters:
Name Type Argument Default Description index
number The index of the tile on the layer.
collides
boolean <optional>
true If true it will enable collision on the tile. If false it will clear collision values from the tile.
layer
number <optional>
The layer to operate on. If not given will default to this.currentLayer.
recalculate
boolean <optional>
true Recalculates the tile faces after the update.
- Source:
-
setLayer(layer)
-
Sets the current layer to the given index.
Parameters:
Name Type Description layer
number | string | Phaser.TilemapLayer The layer to set as current.
- Source:
-
setTileIndexCallback(indexes, callback, callbackContext, layer)
-
Sets a global collision callback for the given tile index within the layer. This will affect all tiles on this layer that have the same index. If a callback is already set for the tile index it will be replaced. Set the callback to null to remove it. If you want to set a callback for a tile at a specific location on the map then see setTileLocationCallback.
Parameters:
Name Type Argument Description indexes
number | array Either a single tile index, or an array of tile indexes to have a collision callback set for.
callback
function The callback that will be invoked when the tile is collided with.
callbackContext
object The context under which the callback is called.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on. If not given will default to this.currentLayer.
- Source:
-
setTileLocationCallback(x, y, width, height, callback, callbackContext, layer)
-
Sets a global collision callback for the given tile index within the layer. This will affect all tiles on this layer that have the same index. If a callback is already set for the tile index it will be replaced. Set the callback to null to remove it. If you want to set a callback for a tile at a specific location on the map then see setTileLocationCallback.
Parameters:
Name Type Argument Description x
number X position of the top left of the area to copy (given in tiles, not pixels)
y
number Y position of the top left of the area to copy (given in tiles, not pixels)
width
number The width of the area to copy (given in tiles, not pixels)
height
number The height of the area to copy (given in tiles, not pixels)
callback
function The callback that will be invoked when the tile is collided with.
callbackContext
object The context under which the callback is called.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on. If not given will default to this.currentLayer.
- Source:
-
shuffle(x, y, width, height, layer)
-
Shuffles a set of tiles in a given area. It will only randomise the tiles in that area, so if they're all the same nothing will appear to have changed!
Parameters:
Name Type Argument Description x
number X position of the top left of the area to operate one, given in tiles, not pixels.
y
number Y position of the top left of the area to operate one, given in tiles, not pixels.
width
number The width in tiles of the area to operate on.
height
number The height in tiles of the area to operate on.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on.
- Source:
-
swapTile(tileA, tileB, x, y, width, height, layer)
-
Scans the given area for tiles with an index matching tileA and swaps them with tileB.
Parameters:
Name Type Argument Description tileA
number First tile index.
tileB
number Second tile index.
x
number X position of the top left of the area to operate one, given in tiles, not pixels.
y
number Y position of the top left of the area to operate one, given in tiles, not pixels.
width
number The width in tiles of the area to operate on.
height
number The height in tiles of the area to operate on.
layer
number | string | Phaser.TilemapLayer <optional>
The layer to operate on.
- Source: