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:
-
currentLayer
-
- Source:
Properties:
Name Type Description currentLayernumber The current layer.
-
debugMap
-
- Source:
Properties:
Name Type Description debugMaparray Map data used for debug values only.
-
game
-
- Source:
Properties:
Name Type Description gamePhaser.Game A reference to the currently running Game.
-
images
-
- Source:
Properties:
Name Type Description imagesarray An array of Tiled Image Layers.
-
key
-
- Source:
Properties:
Name Type Description keystring The key of this map data in the Phaser.Cache.
-
layers
-
- Source:
Properties:
Name Type Description layersarray An array of Tilemap layer data.
-
objects
-
- Source:
Properties:
Name Type Description objectsarray An array of Tiled Object Layers.
-
tiles
-
- Source:
Properties:
Name Type Description tilesarray The super array of Tiles.
-
tilesets
-
- Source:
Properties:
Name Type Description tilesetsarray An array of Tilesets.
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 tilesetstring The name of the tileset as specified in the map data.
keystring <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:
-
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 xnumber X position of the top left of the area to copy (given in tiles, not pixels)
ynumber Y position of the top left of the area to copy (given in tiles, not pixels)
widthnumber The width of the area to copy (given in tiles, not pixels)
heightnumber The height of the area to copy (given in tiles, not pixels)
layernumber | 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 namestring The name of the map (mostly used for debugging)
widthnumber The width of the map in tiles.
heightnumber The height of the map in tiles.
- Source:
-
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 indexnumber The index of the tile that the area will be filled with.
xnumber X position of the top left of the area to operate one, given in tiles, not pixels.
ynumber Y position of the top left of the area to operate one, given in tiles, not pixels.
widthnumber The width in tiles of the area to operate on.
heightnumber The height in tiles of the area to operate on.
layernumber | 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 callbacknumber The callback. Each tile in the given area will be passed to this callback as the first and only parameter.
contextnumber The context under which the callback should be run.
xnumber X position of the top left of the area to operate one, given in tiles, not pixels.
ynumber Y position of the top left of the area to operate one, given in tiles, not pixels.
widthnumber The width in tiles of the area to operate on.
heightnumber The height in tiles of the area to operate on.
layernumber | string | Phaser.TilemapLayer <optional>
The layer to operate on.
- Source:
-
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 xnumber X position to get the tile from (given in tile units, not pixels)
ynumber Y position to get the tile from (given in tile units, not pixels)
layernumber | string | Phaser.TilemapLayer <optional>
The layer to get the tile from.
- Source:
Returns:
The tile at the given coordinates.
- Type
- Phaser.Tile
-
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 xnumber X position to get the tile from (given in pixels)
ynumber Y position to get the tile from (given in pixels)
layernumber | 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 xnumber X position of the top left of the area to paste to (given in tiles, not pixels)
ynumber Y position of the top left of the area to paste to (given in tiles, not pixels)
tileblockarray The block of tiles to paste.
layernumber | 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 tilePhaser.Tile | number The index of this tile to set or a Phaser.Tile object.
xnumber X position to place the tile (given in tile units, not pixels)
ynumber Y position to place the tile (given in tile units, not pixels)
layernumber | 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 tilePhaser.Tile | number The index of this tile to set or a Phaser.Tile object.
xnumber X position to insert the tile (given in pixels)
ynumber Y position to insert the tile (given in pixels)
tileWidthnumber The width of the tile in pixels.
tileHeightnumber The height of the tile in pixels.
layernumber | 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 xnumber X position of the top left of the area to operate one, given in tiles, not pixels.
ynumber Y position of the top left of the area to operate one, given in tiles, not pixels.
widthnumber The width in tiles of the area to operate on.
heightnumber The height in tiles of the area to operate on.
layernumber | 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
sourceand updates their index to matchdest.Parameters:
Name Type Argument Description sourcenumber The tile index value to scan for.
destnumber The tile index value to replace found tiles with.
xnumber X position of the top left of the area to operate one, given in tiles, not pixels.
ynumber Y position of the top left of the area to operate one, given in tiles, not pixels.
widthnumber The width in tiles of the area to operate on.
heightnumber The height in tiles of the area to operate on.
layernumber | string | Phaser.TilemapLayer <optional>
The layer to operate on.
- Source:
-
setLayer(layer)
-
Sets the current layer to the given index.
Parameters:
Name Type Description layernumber | string | Phaser.TilemapLayer The layer to set as current.
- 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 xnumber X position of the top left of the area to operate one, given in tiles, not pixels.
ynumber Y position of the top left of the area to operate one, given in tiles, not pixels.
widthnumber The width in tiles of the area to operate on.
heightnumber The height in tiles of the area to operate on.
layernumber | 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 tileAnumber First tile index.
tileBnumber Second tile index.
xnumber X position of the top left of the area to operate one, given in tiles, not pixels.
ynumber Y position of the top left of the area to operate one, given in tiles, not pixels.
widthnumber The width in tiles of the area to operate on.
heightnumber The height in tiles of the area to operate on.
layernumber | string | Phaser.TilemapLayer <optional>
The layer to operate on.
- Source: