Class: Tilemap

Phaser.Tilemaps. Tilemap

A Tilemap is a container for Tilemap data. This isn't a display object, rather, it holds data
about the map and allows you to add tilesets and tilemap layers to it. A map can have one or
more tilemap layers (StaticTilemapLayer or DynamicTilemapLayer), which are the display
objects that actually render tiles.

The Tilemap data be parsed from a Tiled JSON file, a CSV file or a 2D array. Tiled is a free
software package specifically for creating tile maps, and is available from:
http://www.mapeditor.org

A Tilemap has handy methods for getting & manipulating the tiles within a layer. You can only
use the methods that change tiles (e.g. removeTileAt) on a DynamicTilemapLayer.

Note that all Tilemaps use a base tile size to calculate dimensions from, but that a
StaticTilemapLayer or DynamicTilemapLayer may have its own unique tile size that overrides
it.


new Tilemap(scene, mapData)

Parameters:
Name Type Description
scene Phaser.Scene

The Scene to which this Tilemap belongs.

mapData Phaser.Tilemaps.MapData

A MapData instance containing Tilemap data.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 39)

Members


currentLayerIndex :integer

The index of the currently selected LayerData object.

Type:
  • integer
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 232)

format :number

The format of the map data.

Type:
  • number
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 143)

height :number

The height of the map (in tiles).

Type:
  • number
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 107)

heightInPixels :number

The height of the map in pixels based on height * tileHeight.

Type:
  • number
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 179)

imageCollections :Array.<Phaser.Tilemaps.ImageCollection>

Type:
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 188)

images :array

An array of Tiled Image Layers.

Type:
  • array
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 196)

layer :Phaser.Tilemaps.LayerData

The LayerData object that is currently selected in the map. You can set this property using
any type supported by setLayer.

Type:
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1350)

layers :Array.<Phaser.Tilemaps.LayerData>

An array of Tilemap layer data.

Type:
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 205)

objects :Array.<Phaser.Tilemaps.ObjectLayer>

An array of ObjectLayer instances parsed from Tiled object layers.

Type:
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 223)

orientation :string

The orientation of the map data (as specified in Tiled), usually 'orthogonal'.

Type:
  • string
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 116)

properties :object

Map specific properties as specified in Tiled.

Type:
  • object
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 161)

renderOrder :string

The render (draw) order of the map data (as specified in Tiled), usually 'right-down'.

The draw orders are:

right-down
left-down
right-up
left-up

This can be changed via the setRenderOrder method.

Type:
  • string
Since: 3.12.0
Source: src/tilemaps/Tilemap.js (Line 125)

scene :Phaser.Scene

Type:
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 71)

tileHeight :integer

The base height of a tile in pixels. Note that individual layers may have a different
tile height.

Type:
  • integer
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 88)

tilesets :Array.<Phaser.Tilemaps.Tileset>

An array of Tilesets used in the map.

Type:
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 214)

tileWidth :integer

The base width of a tile in pixels. Note that individual layers may have a different tile
width.

Type:
  • integer
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 78)

version :number

The version of the map data (as specified in Tiled, usually 1).

Type:
  • number
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 152)

width :number

The width of the map (in tiles).

Type:
  • number
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 98)

widthInPixels :number

The width of the map in pixels based on width * tileWidth.

Type:
  • number
Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 170)

Methods


addTilesetImage(tilesetName [, key] [, tileWidth] [, tileHeight] [, tileMargin] [, tileSpacing] [, gid])

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 Default Description
tilesetName 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
undefined or null it will look for an image with a key matching the tilesetName parameter.

tileWidth integer <optional>

The width of the tile (in pixels) in the Tileset Image. If not
given it will default to the map's tileWidth value, or the tileWidth specified in the Tiled
JSON file.

tileHeight integer <optional>

The height of the tiles (in pixels) in the Tileset Image. If
not given it will default to the map's tileHeight value, or the tileHeight specified in the
Tiled JSON file.

tileMargin integer <optional>

The margin around the tiles in the sheet (in pixels). If not
specified, it will default to 0 or the value specified in the Tiled JSON file.

tileSpacing integer <optional>

The spacing between each the tile in the sheet (in pixels).
If not specified, it will default to 0 or the value specified in the Tiled JSON file.

gid integer <optional>
0

If adding multiple tilesets to a blank map, specify the starting
GID this set will use here.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 288)
Returns:

Returns the Tileset object that was created or updated, or null if it
failed.

Type
Phaser.Tilemaps.Tileset

calculateFacesAt(tileX, tileY [, layer])

Calculates interesting faces at the given tile coordinates of the specified layer. Interesting
faces are used internally for optimizing collisions against tiles. This method is mostly used
internally to optimize recalculating faces when only one tile has been changed.

If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Description
tileX integer

The x coordinate, in tiles, not pixels.

tileY integer

The y coordinate, in tiles, not pixels.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1505)
Returns:

Returns this, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

calculateFacesWithin( [tileX] [, tileY] [, width] [, height] [, layer])

Calculates interesting faces within the rectangular area specified (in tile coordinates) of the
layer. Interesting faces are used internally for optimizing collisions against tiles. This method
is mostly used internally.

If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1532)
Returns:

Returns this, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

convertLayerToStatic( [layer])

Turns the StaticTilemapLayer associated with the given layer into a DynamicTilemapLayer. If
no layer specified, the map's current layer is used. This is useful if you want to manipulate
a map at the start of a scene, but then make it non-manipulable and optimize it for speed.
Note: the DynamicTilemapLayer passed in is destroyed, so make sure to store the value
returned from this method if you want to manipulate the new StaticTilemapLayer.

Parameters:
Name Type Argument Description
layer string | integer | Phaser.Tilemaps.DynamicTilemapLayer <optional>

The name of the layer from Tiled, the
index of the layer in the map, or a DynamicTilemapLayer.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 362)
Returns:

Returns the new layer that was created, or null if it
failed.

Type
Phaser.Tilemaps.StaticTilemapLayer

copy(srcTileX, srcTileY, width, height, destTileX, destTileY [, recalculateFaces] [, layer])

Copies the tiles in the source rectangular area to a new destination (all specified in tile
coordinates) within the layer. This copies all tile properties & recalculates collision
information in the destination region.

If no layer specified, the map's current layer is used. This cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
srcTileX integer

The x coordinate of the area to copy from, in tiles, not pixels.

srcTileY integer

The y coordinate of the area to copy from, in tiles, not pixels.

width integer

The width of the area to copy, in tiles, not pixels.

height integer

The height of the area to copy, in tiles, not pixels.

destTileX integer

The x coordinate of the area to copy to, in tiles, not pixels.

destTileY integer

The y coordinate of the area to copy to, in tiles, not pixels.

recalculateFaces boolean <optional>
true

true if the faces data should be recalculated.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 407)
Returns:

Returns this, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

createBlankDynamicLayer(name, tileset [, x] [, y] [, width] [, height] [, tileWidth] [, tileHeight])

Creates a new and empty DynamicTilemapLayer. The currently selected layer in the map is set to this new layer.

Parameters:
Name Type Argument Default Description
name string

The name of this layer. Must be unique within the map.

tileset string | Array.<string> | Phaser.Tilemaps.Tileset | Array.<Phaser.Tilemaps.Tileset>

The tileset, or an array of tilesets, used to render this layer. Can be a string or a Tileset object.

x number <optional>
0

The world x position where the top left of this layer will be placed.

y number <optional>
0

The world y position where the top left of this layer will be placed.

width integer <optional>

The width of the layer in tiles. If not specified, it will default to the map's width.

height integer <optional>

The height of the layer in tiles. If not specified, it will default to the map's height.

tileWidth integer <optional>

The width of the tiles the layer uses for calculations. If not specified, it will default to the map's tileWidth.

tileHeight integer <optional>

The height of the tiles the layer uses for calculations. If not specified, it will default to the map's tileHeight.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 447)
Returns:

Returns the new layer was created, or null if it failed.

Type
Phaser.Tilemaps.DynamicTilemapLayer

createDynamicLayer(layerID, tileset, x, y)

Creates a new DynamicTilemapLayer that renders the LayerData associated with the given
layerID. The currently selected layer in the map is set to this new layer.

The layerID 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.

Unlike a static layer, a dynamic layer can be modified. See DynamicTilemapLayer for more
information.

Parameters:
Name Type Description
layerID integer | string

The layer array index value, or if a string is given, the layer name from Tiled.

tileset string | Array.<string> | Phaser.Tilemaps.Tileset | Array.<Phaser.Tilemaps.Tileset>

The tileset, or an array of tilesets, used to render this layer. Can be a string or a Tileset object.

x number

The x position to place the layer in the world. If not specified, it will default to the layer offset from Tiled or 0.

y number

The y position to place the layer in the world. If not specified, it will default to the layer offset from Tiled or 0.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 516)
Returns:

Returns the new layer was created, or null if it failed.

Type
Phaser.Tilemaps.DynamicTilemapLayer

createFromObjects(name, id, spriteConfig [, scene])

Creates a Sprite for every object matching the given gid in the map data. All properties from
the map data objectgroup are copied into the spriteConfig, 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 of alpha: 0.5 in the map editor will duplicate that when the Sprite is created.

Custom object properties not sharing names with the Sprite's own properties are copied to the
Sprite's data store.

Parameters:
Name Type Argument Default Description
name string

The name of the object layer (from Tiled) to create Sprites from.

id integer | string

Either the id (object), gid (tile object) or name (object or
tile object) from Tiled. Ids are unique in Tiled, but a gid is shared by all tile objects
with the same graphic. The same name can be used on multiple objects.

spriteConfig SpriteConfig

The config object to pass into the Sprite creator (i.e.
scene.make.sprite).

scene Phaser.Scene <optional>
the scene the map is within

The Scene to create the Sprites within.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 571)
Returns:

An array of the Sprites that were created.

Type
Array.<Phaser.GameObjects.Sprite>

createFromTiles(indexes, replacements, spriteConfig [, scene] [, camera] [, layer])

Creates a Sprite for every object matching the given tile indexes in the layer. You can
optionally specify if each tile will be replaced with a new tile after the Sprite has been
created. This is useful if you want to lay down special tiles in a level that are converted to
Sprites, but want to replace the tile itself with a floor tile or similar once converted.

Parameters:
Name Type Argument Default Description
indexes integer | array

The tile index, or array of indexes, to create Sprites from.

replacements integer | array

The tile index, or array of indexes, to change a converted
tile to. Set to null to leave the tiles unchanged. If an array is given, it is assumed to be a
one-to-one mapping with the indexes array.

spriteConfig SpriteConfig

The config object to pass into the Sprite creator (i.e. scene.make.sprite).

scene Phaser.Scene <optional>
scene the map is within

The Scene to create the Sprites within.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when calculating the tile index from the world values.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 675)
Returns:

Returns an array of Tiles, or null if the layer given was invalid.

Type
Array.<Phaser.GameObjects.Sprite>

createStaticLayer(layerID, tileset, x, y)

Creates a new StaticTilemapLayer that renders the LayerData associated with the given
layerID. The currently selected layer in the map is set to this new layer.

The layerID 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.

It's important to remember that a static layer cannot be modified. See StaticTilemapLayer for
more information.

Parameters:
Name Type Description
layerID integer | string

The layer array index value, or if a string is given, the layer name from Tiled.

tileset string | Array.<string> | Phaser.Tilemaps.Tileset | Array.<Phaser.Tilemaps.Tileset>

The tileset, or an array of tilesets, used to render this layer. Can be a string or a Tileset object.

x number

The x position to place the layer in the world. If not specified, it will default to the layer offset from Tiled or 0.

y number

The y position to place the layer in the world. If not specified, it will default to the layer offset from Tiled or 0.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 704)
Returns:

Returns the new layer was created, or null if it failed.

Type
Phaser.Tilemaps.StaticTilemapLayer

destroy()

Removes all layer data from this Tilemap and nulls the scene reference. This will destroy any
StaticTilemapLayers or DynamicTilemapLayers that have been linked to LayerData.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 759)

fill(index [, tileX] [, tileY] [, width] [, height] [, recalculateFaces] [, layer])

Sets the tiles in the given rectangular area (in tile coordinates) of the layer with the
specified index. Tiles will be set to collide if the given index is a colliding index.
Collision information in the region will be recalculated.

If no layer specified, the map's current layer is used.
This cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
index integer

The tile index to fill the area with.

tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

recalculateFaces boolean <optional>
true

true if the faces data should be recalculated.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 774)
Returns:

Returns this, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

filterObjects(objectLayer, callback [, context])

For each object in the given object layer, run the given filter callback function. Any
objects that pass the filter test (i.e. where the callback returns true) will returned as a
new array. Similar to Array.prototype.Filter in vanilla JS.

Parameters:
Name Type Argument Description
objectLayer Phaser.Tilemaps.ObjectLayer | string

The name of an object layer (from Tiled) or an ObjectLayer instance.

callback TilemapFilterCallback

The callback. Each object in the given area will be passed to this callback as the first and only parameter.

context object <optional>

The context under which the callback should be run.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 809)
Returns:

An array of object that match the search, or null if the objectLayer given was invalid.

Type
Array.<Phaser.GameObjects.GameObject>

filterTiles(callback [, context] [, tileX] [, tileY] [, width] [, height] [, filteringOptions] [, layer])

For each tile in the given rectangular area (in tile coordinates) of the layer, run the given
filter callback function. Any tiles that pass the filter test (i.e. where the callback returns
true) will returned as a new array. Similar to Array.prototype.Filter in vanilla JS.
If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
callback function

The callback. Each tile in the given area will be passed to this
callback as the first and only parameter. The callback should return true for tiles that pass the
filter.

context object <optional>

The context under which the callback should be run.

tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area to filter.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area to filter.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

filteringOptions object <optional>

Optional filters to apply when getting the tiles.

Properties
Name Type Argument Default Description
isNotEmpty boolean <optional>
false

If true, only return tiles that don't have -1 for an index.

isColliding boolean <optional>
false

If true, only return tiles that collide on at least one side.

hasInterestingFace boolean <optional>
false

If true, only return tiles that have at least one interesting face.

layer Phaser.Tilemaps.LayerData <optional>

The Tile layer to apply the filter on. If not provided will use the current layer.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 841)
Returns:

Returns an array of Tiles, or null if the layer given was invalid.

Type
Array.<Phaser.Tilemaps.Tile>

findByIndex(index [, skip] [, reverse] [, layer])

Searches the entire map layer for the first tile matching the given index, then returns that Tile
object. If no match is found, it returns null. The search starts from the top-left tile and
continues horizontally until it hits the end of the row, then it drops down to the next column.
If the reverse boolean is true, it scans starting from the bottom-right corner traveling up to
the top-left.
If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
index integer

The tile index value to search for.

skip integer <optional>
0

The number of times to skip a matching tile before returning.

reverse boolean <optional>
false

If true it will scan the layer in reverse, starting at the bottom-right. Otherwise it scans from the top-left.

layer Phaser.Tilemaps.LayerData <optional>

The Tile layer to run the search on. If not provided will use the current layer.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 875)
Returns:

Returns a Tiles, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tile

findObject(objectLayer, callback [, context])

Find the first object in the given object layer that satisfies the provided testing function.
I.e. finds the first object for which callback returns true. Similar to
Array.prototype.find in vanilla JS.

Parameters:
Name Type Argument Description
objectLayer Phaser.Tilemaps.ObjectLayer | string

The name of an object layer (from Tiled) or an ObjectLayer instance.

callback TilemapFindCallback

The callback. Each object in the given area will be passed to this callback as the first and only parameter.

context object <optional>

The context under which the callback should be run.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 902)
Returns:

An object that matches the search, or null if no object found.

Type
Phaser.GameObjects.GameObject

findTile(callback [, context] [, tileX] [, tileY] [, width] [, height] [, filteringOptions] [, layer])

Find the first tile in the given rectangular area (in tile coordinates) of the layer that
satisfies the provided testing function. I.e. finds the first tile for which callback returns
true. Similar to Array.prototype.find in vanilla JS.
If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
callback FindTileCallback

The callback. Each tile in the given area will be passed to this callback as the first and only parameter.

context object <optional>

The context under which the callback should be run.

tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area to search.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area to search.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

filteringOptions object <optional>

Optional filters to apply when getting the tiles.

Properties
Name Type Argument Default Description
isNotEmpty boolean <optional>
false

If true, only return tiles that don't have -1 for an index.

isColliding boolean <optional>
false

If true, only return tiles that collide on at least one side.

hasInterestingFace boolean <optional>
false

If true, only return tiles that have at least one interesting face.

layer Phaser.Tilemaps.LayerData <optional>

The Tile layer to run the search on. If not provided will use the current layer.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 934)
Returns:

Returns a Tiles, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tile

forEachTile(callback [, context] [, tileX] [, tileY] [, width] [, height] [, filteringOptions] [, layer])

For each tile in the given rectangular area (in tile coordinates) of the layer, run the given
callback. Similar to Array.prototype.forEach in vanilla JS.

If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
callback EachTileCallback

The callback. Each tile in the given area will be passed to this callback as the first and only parameter.

context object <optional>

The context under which the callback should be run.

tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area to search.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area to search.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

filteringOptions object <optional>

Optional filters to apply when getting the tiles.

Properties
Name Type Argument Default Description
isNotEmpty boolean <optional>
false

If true, only return tiles that don't have -1 for an index.

isColliding boolean <optional>
false

If true, only return tiles that collide on at least one side.

hasInterestingFace boolean <optional>
false

If true, only return tiles that have at least one interesting face.

layer Phaser.Tilemaps.LayerData <optional>

The Tile layer to run the search on. If not provided will use the current layer.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 966)
Returns:

Returns this, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

getImageIndex(name)

Gets the image layer index based on its name.

Parameters:
Name Type Description
name string

The name of the image to get.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1001)
Returns:

The index of the image in this tilemap, or null if not found.

Type
integer

getIndex(location, name)

Internally used. Returns the index of the object in one of the Tilemaps arrays whose name
property matches the given name.

Parameters:
Name Type Description
location array

The Tilemap array to search.

name string

The name of the array element to get.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1016)
Returns:

The index of the element in the array, or null if not found.

Type
number

getLayer( [layer])

Gets the LayerData from this.layers that is associated with layer, or null if an invalid
layer is given.

Parameters:
Name Type Argument Description
layer string | integer | Phaser.Tilemaps.DynamicTilemapLayer | Phaser.Tilemaps.StaticTilemapLayer <optional>

The name of the
layer from Tiled, the index of the layer in the map, a DynamicTilemapLayer or a
StaticTilemapLayer. If not given will default to the maps current layer index.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1041)
Returns:

The corresponding LayerData within this.layers.

Type
Phaser.Tilemaps.LayerData

getLayerIndex( [layer])

Gets the LayerData index of the given layer within this.layers, or null if an invalid
layer is given.

Parameters:
Name Type Argument Description
layer string | integer | Phaser.Tilemaps.DynamicTilemapLayer | Phaser.Tilemaps.StaticTilemapLayer <optional>

The name of the
layer from Tiled, the index of the layer in the map, a DynamicTilemapLayer or a
StaticTilemapLayer. If not given will default to the map's current layer index.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1079)
Returns:

The LayerData index within this.layers.

Type
integer

getLayerIndexByName(name)

Gets the index of the LayerData within this.layers that has the given name, or null if an
invalid name is given.

Parameters:
Name Type Description
name string

The name of the layer to get.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1116)
Returns:

The LayerData index within this.layers.

Type
integer

getObjectLayer( [name])

Gets the ObjectLayer from this.objects that has the given name, or null if no ObjectLayer
is found with that name.

Parameters:
Name Type Argument Description
name string <optional>

The name of the object layer from Tiled.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1061)
Returns:

The corresponding ObjectLayer within this.objects or null.

Type
Phaser.Tilemaps.ObjectLayer

getTileAt(tileX, tileY [, nonNull] [, layer])

Gets a tile at the given tile coordinates from the given layer.
If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
tileX integer

X position to get the tile from (given in tile units, not pixels).

tileY integer

Y position to get the tile from (given in tile units, not pixels).

nonNull boolean <optional>
false

If true getTile won't return null for empty tiles, but a Tile object with an index of -1.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1132)
Returns:

Returns a Tile, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tile

getTileAtWorldXY(worldX, worldY [, nonNull] [, camera] [, layer])

Gets a tile at the given world coordinates from the given layer.
If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
worldX number

X position to get the tile from (given in pixels)

worldY number

Y position to get the tile from (given in pixels)

nonNull boolean <optional>
false

If true, function won't return null for empty tiles, but a Tile object with an index of -1.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when calculating the tile index from the world values.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1155)
Returns:

Returns a Tile, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tile

getTileset(name)

Gets the Tileset that has the given name, or null if an invalid name is given.

Parameters:
Name Type Description
name string

The name of the Tileset to get.

Since: 3.14.0
Source: src/tilemaps/Tilemap.js (Line 1268)
Returns:

The Tileset, or null if no matching named tileset was found.

Type
Phaser.Tilemaps.Tileset

getTilesetIndex(name)

Gets the index of the Tileset within this.tilesets that has the given name, or null if an
invalid name is given.

Parameters:
Name Type Description
name string

The name of the Tileset to get.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1285)
Returns:

The Tileset index within this.tilesets.

Type
integer

getTilesWithin( [tileX] [, tileY] [, width] [, height] [, filteringOptions] [, layer])

Gets the tiles in the given rectangular area (in tile coordinates) of the layer.
If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

filteringOptions object <optional>

Optional filters to apply when getting the tiles.

Properties
Name Type Argument Default Description
isNotEmpty boolean <optional>
false

If true, only return tiles that don't have -1 for an index.

isColliding boolean <optional>
false

If true, only return tiles that collide on at least one side.

hasInterestingFace boolean <optional>
false

If true, only return tiles that have at least one interesting face.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1184)
Returns:

Returns an array of Tiles, or null if the layer given was invalid.

Type
Array.<Phaser.Tilemaps.Tile>

getTilesWithinShape(shape [, filteringOptions] [, camera] [, layer])

Gets the tiles that overlap with the given shape in the given layer. The shape must be a Circle,
Line, Rectangle or Triangle. The shape should be in world coordinates.
If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
shape Phaser.Geom.Circle | Phaser.Geom.Line | Phaser.Geom.Rectangle | Phaser.Geom.Triangle

A shape in world (pixel) coordinates

filteringOptions object <optional>

Optional filters to apply when getting the tiles.

Properties
Name Type Argument Default Description
isNotEmpty boolean <optional>
false

If true, only return tiles that don't have -1 for an index.

isColliding boolean <optional>
false

If true, only return tiles that collide on at least one side.

hasInterestingFace boolean <optional>
false

If true, only return tiles that have at least one interesting face.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when factoring in which tiles to return.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to search. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1212)
Returns:

Returns an array of Tiles, or null if the layer given was invalid.

Type
Array.<Phaser.Tilemaps.Tile>

getTilesWithinWorldXY(worldX, worldY, width, height [, filteringOptions] [, camera] [, layer])

Gets the tiles in the given rectangular area (in world coordinates) of the layer.
If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
worldX number

The world x coordinate for the top-left of the area.

worldY number

The world y coordinate for the top-left of the area.

width number

The width of the area.

height number

The height of the area.

filteringOptions object <optional>

Optional filters to apply when getting the tiles.

Properties
Name Type Argument Default Description
isNotEmpty boolean <optional>
false

If true, only return tiles that don't have -1 for an index.

isColliding boolean <optional>
false

If true, only return tiles that collide on at least one side.

hasInterestingFace boolean <optional>
false

If true, only return tiles that have at least one interesting face.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when factoring in which tiles to return.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to search. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1239)
Returns:

Returns an array of Tiles, or null if the layer given was invalid.

Type
Array.<Phaser.Tilemaps.Tile>

hasTileAt(tileX, tileY [, layer])

Checks if there is a tile at the given location (in tile coordinates) in the given layer. Returns
false if there is no tile or if the tile at that location has an index of -1.

If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Description
tileX integer

The x coordinate, in tiles, not pixels.

tileY integer

The y coordinate, in tiles, not pixels.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to search. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1301)
Returns:

Returns a boolean, or null if the layer given was invalid.

Type
boolean

hasTileAtWorldXY(worldX, worldY [, camera] [, layer])

Checks if there is a tile at the given location (in world coordinates) in the given layer. Returns
false if there is no tile or if the tile at that location has an index of -1.

If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
worldX number

The x coordinate, in pixels.

worldY number

The y coordinate, in pixels.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when factoring in which tiles to return.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to search. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1325)
Returns:

Returns a boolean, or null if the layer given was invalid.

Type
boolean

putTileAt(tile, tileX, tileY [, recalculateFaces] [, layer])

Puts a tile at the given tile coordinates in the specified layer. You can pass in either an index
or a Tile object. If you pass in a Tile, all attributes will be copied over to the specified
location. If you pass in an index, only the index at the specified location will be changed.
Collision information will be recalculated at the specified location.

If no layer specified, the maps current layer is used.

This cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
tile integer | Phaser.Tilemaps.Tile

The index of this tile to set or a Tile object.

tileX integer

The x coordinate, in tiles, not pixels.

tileY integer

The y coordinate, in tiles, not pixels.

recalculateFaces boolean <optional>
true

true if the faces data should be recalculated.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1370)
Returns:

Returns a Tile, or null if the layer given was invalid or the coordinates were out of bounds.

Type
Phaser.Tilemaps.Tile

putTileAtWorldXY(tile, worldX, worldY [, recalculateFaces] [, camera] [, layer])

Puts a tile at the given world coordinates (pixels) in the specified layer. You can pass in either
an index or a Tile object. If you pass in a Tile, all attributes will be copied over to the
specified location. If you pass in an index, only the index at the specified location will be
changed. Collision information will be recalculated at the specified location.

If no layer specified, the maps current layer is used. This
cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
tile integer | Phaser.Tilemaps.Tile

The index of this tile to set or a Tile object.

worldX number

The x coordinate, in pixels.

worldY number

The y coordinate, in pixels.

recalculateFaces boolean <optional>
true

true if the faces data should be recalculated.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when calculating the tile index from the world values.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1402)
Returns:

Returns a Tile, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tile

putTilesAt(tile, tileX, tileY [, recalculateFaces] [, layer])

Puts an array of tiles or a 2D array of tiles at the given tile coordinates in the specified
layer. The array can be composed of either tile indexes or Tile objects. If you pass in a Tile,
all attributes will be copied over to the specified location. If you pass in an index, only the
index at the specified location will be changed. Collision information will be recalculated
within the region tiles were changed.

If no layer specified, the maps current layer is used.
This cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
tile Array.<integer> | Array.<Array.<integer>> | Array.<Phaser.Tilemaps.Tile> | Array.<Array.<Phaser.Tilemaps.Tile>>

A row (array) or grid (2D array) of Tiles or tile indexes to place.

tileX integer

The x coordinate, in tiles, not pixels.

tileY integer

The y coordinate, in tiles, not pixels.

recalculateFaces boolean <optional>
true

true if the faces data should be recalculated.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1434)
Returns:

Returns this, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

randomize( [tileX] [, tileY] [, width] [, height] [, indexes] [, layer])

Randomizes the indexes of a rectangular region of tiles (in tile coordinates) within the
specified layer. Each tile will recieve a new index. If an array of indexes is passed in, then
those will be used for randomly assigning new tile indexes. If an array is not provided, the
indexes found within the region (excluding -1) will be used for randomly assigning new tile
indexes. This method only modifies tile indexes and does not change collision information.

If no layer specified, the maps current layer is used.
This cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

indexes Array.<integer> <optional>

An array of indexes to randomly draw from during randomization.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1469)
Returns:

Returns this, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

removeAllLayers()

Removes all layers from this Tilemap and destroys any associated StaticTilemapLayers or
DynamicTilemapLayers.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1561)
Returns:

This Tilemap object.

Type
Phaser.Tilemaps.Tilemap

removeTileAt(tileX, tileY [, replaceWithNull] [, recalculateFaces] [, layer])

Removes the tile at the given tile coordinates in the specified layer and updates the layer's
collision information.

If no layer specified, the maps current layer is used.
This cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
tileX integer

The x coordinate, in tiles, not pixels.

tileY integer

The y coordinate, in tiles, not pixels.

replaceWithNull boolean <optional>
true

If true, this will replace the tile at the specified location with null instead of a Tile with an index of -1.

recalculateFaces boolean <optional>
true

true if the faces data should be recalculated.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to search. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1587)
Returns:

Returns a Tile, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tile

removeTileAtWorldXY(worldX, worldY [, replaceWithNull] [, recalculateFaces] [, camera] [, layer])

Removes the tile at the given world coordinates in the specified layer and updates the layer's
collision information.

If no layer specified, the maps current layer is used.
This cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
worldX number

The x coordinate, in pixels.

worldY number

The y coordinate, in pixels.

replaceWithNull boolean <optional>
true

If true, this will replace the tile at the specified location with null instead of a Tile with an index of -1.

recalculateFaces boolean <optional>
true

true if the faces data should be recalculated.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when calculating the tile index from the world values.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1616)
Returns:

Returns a Tile, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tile

renderDebug(graphics, styleConfig [, layer])

Draws a debug representation of the layer to the given Graphics. This is helpful when you want to
get a quick idea of which of your tiles are colliding and which have interesting faces. The tiles
are drawn starting at (0, 0) in the Graphics, allowing you to place the debug representation
wherever you want on the screen.

If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Description
graphics Phaser.GameObjects.Graphics

The target Graphics object to draw upon.

styleConfig object

An object specifying the colors to use for the debug drawing.

Properties
Name Type Argument Default Description
tileColor Color <optional>
<nullable>
blue

Color to use for drawing a filled rectangle at non-colliding tile locations. If set to null, non-colliding tiles will not be drawn.

collidingTileColor Color <optional>
<nullable>
orange

Color to use for drawing a filled rectangle at colliding tile locations. If set to null, colliding tiles will not be drawn.

faceColor Color <optional>
<nullable>
grey

Color to use for drawing a line at interesting tile faces. If set to null, interesting tile faces will not be drawn.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to search. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1646)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

replaceByIndex(findIndex, newIndex [, tileX] [, tileY] [, width] [, height] [, layer])

Scans the given rectangular area (given in tile coordinates) for tiles with an index matching
findIndex and updates their index to match newIndex. This only modifies the index and does
not change collision information.

If no layer specified, the maps current layer is used.
This cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
findIndex integer

The index of the tile to search for.

newIndex integer

The index of the tile to replace it with.

tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1677)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

setBaseTileSize(tileWidth, tileHeight)

Sets the base tile size for the map. Note: this does not necessarily match the tileWidth and
tileHeight for all layers. This also updates the base size on all tiles across all layers.

Parameters:
Name Type Description
tileWidth integer

The width of the tiles the map uses for calculations.

tileHeight integer

The height of the tiles the map uses for calculations.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1942)
Returns:

This Tilemap object.

Type
Phaser.Tilemaps.Tilemap

setCollision(indexes [, collides] [, recalculateFaces] [, layer])

Sets collision on the given tile or tiles within a layer by index. 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).

If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
indexes integer | array

Either a single tile index, or an array of tile indexes.

collides boolean <optional>
true

If true it will enable collision. If false it will clear collision.

recalculateFaces boolean <optional>
true

Whether or not to recalculate the tile faces after the update.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1712)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

setCollisionBetween(start, stop [, collides] [, recalculateFaces] [, layer])

Sets collision on a range of tiles in a layer whose index is between the specified start and
stop (inclusive). 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).

If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
start integer

The first index of the tile to be set for collision.

stop integer

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.

recalculateFaces boolean <optional>
true

Whether or not to recalculate the tile faces after the update.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1740)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

setCollisionByExclusion(indexes [, collides] [, recalculateFaces] [, layer])

Sets collision on all tiles in the given layer, except for tiles that have an index specified in
the given array. The collides parameter controls if collision will be enabled (true) or
disabled (false).

If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
indexes Array.<integer>

An array of the tile indexes to not be counted for collision.

collides boolean <optional>
true

If true it will enable collision. If false it will clear collision.

recalculateFaces boolean <optional>
true

Whether or not to recalculate the tile faces after the update.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1802)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

setCollisionByProperty(properties [, collides] [, recalculateFaces] [, layer])

Sets collision on the tiles within a layer by checking tile properties. If a tile has a property
that matches the given properties object, its collision flag will be set. The collides
parameter controls if collision will be enabled (true) or disabled (false). Passing in
{ collides: true } would update the collision flag on any tiles with a "collides" property that
has a value of true. Any tile that doesn't have "collides" set to true will be ignored. You can
also use an array of values, e.g. { types: ["stone", "lava", "sand" ] }. If a tile has a
"types" property that matches any of those values, its collision flag will be updated.

If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
properties object

An object with tile properties and corresponding values that should be checked.

collides boolean <optional>
true

If true it will enable collision. If false it will clear collision.

recalculateFaces boolean <optional>
true

Whether or not to recalculate the tile faces after the update.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1770)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

setCollisionFromCollisionGroup( [collides] [, recalculateFaces] [, layer])

Sets collision on the tiles within a layer by checking each tile's collision group data
(typically defined in Tiled within the tileset collision editor). If any objects are found within
a tile's collision group, the tile's colliding information will be set. The collides parameter
controls if collision will be enabled (true) or disabled (false).

If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Default Description
collides boolean <optional>
true

If true it will enable collision. If false it will clear collision.

recalculateFaces boolean <optional>
true

Whether or not to recalculate the tile faces after the update.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1830)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

setLayer( [layer])

Sets the current layer to the LayerData associated with layer.

Parameters:
Name Type Argument Description
layer string | integer | Phaser.Tilemaps.DynamicTilemapLayer | Phaser.Tilemaps.StaticTilemapLayer <optional>

The name of the
layer from Tiled, the index of the layer in the map, a DynamicTilemapLayer or a
StaticTilemapLayer. If not given will default to the map's current layer index.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1918)
Returns:

This Tilemap object.

Type
Phaser.Tilemaps.Tilemap

setLayerTileSize(tileWidth, tileHeight [, layer])

Sets the tile size for a specific layer. Note: this does not necessarily match the map's
tileWidth and tileHeight for all layers. This will set the tile size for the layer and any
tiles the layer has.

Parameters:
Name Type Argument Description
tileWidth integer

The width of the tiles (in pixels) in the layer.

tileHeight integer

The height of the tiles (in pixels) in the layer.

layer string | integer | Phaser.Tilemaps.DynamicTilemapLayer | Phaser.Tilemaps.StaticTilemapLayer <optional>

The name of the
layer from Tiled, the index of the layer in the map, a DynamicTilemapLayer or a
StaticTilemapLayer. If not given will default to the map's current layer index.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1988)
Returns:

This Tilemap object.

Type
Phaser.Tilemaps.Tilemap

setRenderOrder(renderOrder)

Sets the rendering (draw) order of the tiles in this map.

The default is 'right-down', meaning it will order the tiles starting from the top-left,
drawing to the right and then moving down to the next row.

The draw orders are:

0 = right-down
1 = left-down
2 = right-up
3 = left-up

Setting the render order does not change the tiles or how they are stored in the layer,
it purely impacts the order in which they are rendered.

You can provide either an integer (0 to 3), or the string version of the order.

Calling this method after creating Static or Dynamic Tilemap Layers will not automatically
update them to use the new render order. If you call this method after creating layers, use their
own setRenderOrder methods to change them as needed.

Parameters:
Name Type Description
renderOrder integer | string

The render (draw) order value. Either an integer between 0 and 3, or a string: 'right-down', 'left-down', 'right-up' or 'left-up'.

Since: 3.12.0
Source: src/tilemaps/Tilemap.js (Line 242)
Returns:

This Tilemap object.

Type
Phaser.Tilemaps.Tilemap

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.

If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Description
indexes integer | 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 Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1858)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

setTileLocationCallback(tileX, tileY, width, height, callback [, callbackContext] [, layer])

Sets a collision callback for the given rectangular area (in tile coordindates) within the layer.
If a callback is already set for the tile index it will be replaced. Set the callback to null to
remove it.

If no layer specified, the map's current layer is used.

Parameters:
Name Type Argument Description
tileX integer

The left most tile index (in tile coordinates) to use as the origin of the area.

tileY integer

The top most tile index (in tile coordinates) to use as the origin of the area.

width integer

How many tiles wide from the tileX index the area will be.

height integer

How many tiles tall from the tileY index the area will be.

callback function

The callback that will be invoked when the tile is collided with.

callbackContext object <optional>

The context under which the callback is called.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 1887)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

shuffle( [tileX] [, tileY] [, width] [, height] [, layer])

Shuffles the tiles in a rectangular region (specified in tile coordinates) within the given
layer. It will only randomize the tiles in that area, so if they're all the same nothing will
appear to have changed! This method only modifies tile indexes and does not change collision
information.

If no layer specified, the maps current layer is used.
This cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 2030)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

swapByIndex(tileA, tileB [, tileX] [, tileY] [, width] [, height] [, layer])

Scans the given rectangular area (given in tile coordinates) for tiles with an index matching
indexA and swaps then with indexB. This only modifies the index and does not change collision
information.

If no layer specified, the maps current layer is used.
This cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
tileA integer

First tile index.

tileB integer

Second tile index.

tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 2064)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

tileToWorldX(tileX [, camera] [, layer])

Converts from tile X coordinates (tile units) to world X coordinates (pixels), factoring in the
layers position, scale and scroll.

If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
tileX integer

The x coordinate, in tiles, not pixels.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when calculating the tile index from the world values.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 2099)
Returns:

Returns a number, or null if the layer given was invalid.

Type
number

tileToWorldXY(tileX, tileY [, point] [, camera] [, layer])

Converts from tile XY coordinates (tile units) to world XY coordinates (pixels), factoring in the
layers position, scale and scroll. This will return a new Vector2 object or update the given
point object.

If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
tileX integer

The x coordinate, in tiles, not pixels.

tileY integer

The y coordinate, in tiles, not pixels.

point Phaser.Math.Vector2 <optional>

A Vector2 to store the coordinates in. If not given a new Vector2 is created.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when calculating the tile index from the world values.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 2147)
Returns:

Returns a point, or null if the layer given was invalid.

Type
Phaser.Math.Vector2

tileToWorldY(tileY [, camera] [, layer])

Converts from tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the
layers position, scale and scroll.

If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
tileY integer

The y coordinate, in tiles, not pixels.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when calculating the tile index from the world values.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 2123)
Returns:

Returns a number, or null if the layer given was invalid.

Type
number

weightedRandomize( [tileX] [, tileY] [, width] [, height] [, weightedIndexes] [, layer])

Randomizes the indexes of a rectangular region of tiles (in tile coordinates) within the
specified layer. Each tile will receive a new index. New indexes are drawn from the given
weightedIndexes array. An example weighted array:

[
{ index: 6, weight: 4 }, // Probability of index 6 is 4 / 8
{ index: 7, weight: 2 }, // Probability of index 7 would be 2 / 8
{ index: 8, weight: 1.5 }, // Probability of index 8 would be 1.5 / 8
{ index: 26, weight: 0.5 } // Probability of index 27 would be 0.5 / 8
]

The probability of any index being choose is (the index's weight) / (sum of all weights). This
method only modifies tile indexes and does not change collision information.

If no layer specified, the map's current layer is used. This
cannot be applied to StaticTilemapLayers.

Parameters:
Name Type Argument Default Description
tileX integer <optional>
0

The left most tile index (in tile coordinates) to use as the origin of the area.

tileY integer <optional>
0

The top most tile index (in tile coordinates) to use as the origin of the area.

width integer <optional>
max width based on tileX

How many tiles wide from the tileX index the area will be.

height integer <optional>
max height based on tileY

How many tiles tall from the tileY index the area will be.

weightedIndexes Array.<object> <optional>

An array of objects to randomly draw from during
randomization. They should be in the form: { index: 0, weight: 4 } or
{ index: [0, 1], weight: 4 } if you wish to draw from multiple tile indexes.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 2174)
Returns:

Return this Tilemap object, or null if the layer given was invalid.

Type
Phaser.Tilemaps.Tilemap

worldToTileX(worldX [, snapToFloor] [, camera] [, layer])

Converts from world X coordinates (pixels) to tile X coordinates (tile units), factoring in the
layers position, scale and scroll.

If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
worldX number

The x coordinate to be converted, in pixels, not tiles.

snapToFloor boolean <optional>
true

Whether or not to round the tile coordinate down to the nearest integer.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when calculating the tile index from the world values.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 2220)
Returns:

Returns a number, or null if the layer given was invalid.

Type
number

worldToTileXY(worldX, worldY [, snapToFloor] [, point] [, camera] [, layer])

Converts from world XY coordinates (pixels) to tile XY coordinates (tile units), factoring in the
layers position, scale and scroll. This will return a new Vector2 object or update the given
point object.

If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
worldX number

The x coordinate to be converted, in pixels, not tiles.

worldY number

The y coordinate to be converted, in pixels, not tiles.

snapToFloor boolean <optional>
true

Whether or not to round the tile coordinate down to the nearest integer.

point Phaser.Math.Vector2 <optional>

A Vector2 to store the coordinates in. If not given a new Vector2 is created.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when calculating the tile index from the world values.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 2270)
Returns:

Returns a point, or null if the layer given was invalid.

Type
Phaser.Math.Vector2

worldToTileY(worldY [, snapToFloor] [, camera] [, layer])

Converts from world Y coordinates (pixels) to tile Y coordinates (tile units), factoring in the
layers position, scale and scroll.

If no layer specified, the maps current layer is used.

Parameters:
Name Type Argument Default Description
worldY number

The y coordinate to be converted, in pixels, not tiles.

snapToFloor boolean <optional>
true

Whether or not to round the tile coordinate down to the nearest integer.

camera Phaser.Cameras.Scene2D.Camera <optional>
main camera

The Camera to use when calculating the tile index from the world values.

layer Phaser.Tilemaps.LayerData <optional>

The tile layer to use. If not given the current layer is used.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 2245)
Returns:

Returns a number, or null if the layer given was invalid.

Type
number