Skip to main content
Version: Phaser v4.0.0-rc.6

TilemapLayerBase

A TilemapLayer is a Game Object that renders LayerData from a Tilemap

when used in combination with one, or more, Tilesets.

This is a generic base class that is extended by the TilemapLayer classes.

It is not used directly and should not be instantiated.

Constructor

new TilemapLayerBase(scene, tilemap, layerIndex, [x], [y])

Parameters

nametypeoptionaldefaultdescription
scenePhaser.SceneNoThe Scene to which this Game Object belongs.
tilemapPhaser.Tilemaps.TilemapNoThe Tilemap this layer is a part of.
layerIndexnumberNoThe index of the LayerData associated with this layer.
xnumberYes0The world x position where the top left of this layer will be placed.
ynumberYes0The world y position where the top left of this layer will be placed.

Scope: static

Extends

Phaser.GameObjects.GameObject
Phaser.GameObjects.Components.Alpha
Phaser.GameObjects.Components.BlendMode
Phaser.GameObjects.Components.ComputedSize
Phaser.GameObjects.Components.Depth
Phaser.GameObjects.Components.ElapseTimer
Phaser.GameObjects.Components.Flip
Phaser.GameObjects.Components.GetBounds
Phaser.GameObjects.Components.Lighting
Phaser.GameObjects.Components.Mask
Phaser.GameObjects.Components.Origin
Phaser.GameObjects.Components.RenderNodes
Phaser.GameObjects.Components.ScrollFactor
Phaser.GameObjects.Components.Transform
Phaser.GameObjects.Components.Visible
Phaser.Physics.Arcade.Components.Collision

Source: src/tilemaps/TilemapLayerBase.js#L14
Since: 4.0.0

Inherited Members

From Phaser.GameObjects.Components.Alpha:

From Phaser.GameObjects.Components.BlendMode:

From Phaser.GameObjects.Components.ComputedSize:

From Phaser.GameObjects.Components.Depth:

From Phaser.GameObjects.Components.ElapseTimer:

From Phaser.GameObjects.Components.Filters:

From Phaser.GameObjects.Components.Flip:

From Phaser.GameObjects.Components.Lighting:

From Phaser.GameObjects.Components.Mask:

From Phaser.GameObjects.Components.RenderNodes:

From Phaser.GameObjects.Components.ScrollFactor:

From Phaser.GameObjects.Components.Transform:

From Phaser.GameObjects.Components.Visible:

From Phaser.GameObjects.GameObject:


Public Members

collisionCategory

collisionCategory: number

Description:

The Tilemap Layer Collision Category.

This is exclusively used by the Arcade Physics system.

This can be set to any valid collision bitfield value.

See the setCollisionCategory method for more details.

Source: src/tilemaps/TilemapLayerBase.js#L140
Since: 3.70.0


collisionMask

collisionMask: number

Description:

The Tilemap Layer Collision Mask.

This is exclusively used by the Arcade Physics system.

See the setCollidesWith method for more details.

Source: src/tilemaps/TilemapLayerBase.js#L155
Since: 3.70.0


displayOriginX

displayOriginX: number

Description:

The horizontal display origin of this Tilemap Layer.

Overrides: Phaser.GameObjects.Components.Origin#displayOriginX

Source: src/tilemaps/TilemapLayerBase.js#L188
Since: 3.0.0


displayOriginY

displayOriginY: number

Description:

The vertical display origin of this Tilemap Layer.

Overrides: Phaser.GameObjects.Components.Origin#displayOriginY

Source: src/tilemaps/TilemapLayerBase.js#L198
Since: 3.0.0


gidMap

gidMap: Array.<Phaser.Tilemaps.Tileset>

Description:

An array holding the mapping between the tile indexes and the tileset they belong to.

Source: src/tilemaps/TilemapLayerBase.js#L121
Since: 3.50.0


isTilemap

isTilemap: boolean

Description:

Used internally by physics system to perform fast type checks.

Source: src/tilemaps/TilemapLayerBase.js#L80
Since: 3.50.0


layer

layer: Phaser.Tilemaps.LayerData

Description:

The LayerData associated with this layer. LayerData can only be associated with one

tilemap layer.

Source: src/tilemaps/TilemapLayerBase.js#L108
Since: 3.50.0


layerIndex

layerIndex: number

Description:

The index of the LayerData associated with this layer.

Source: src/tilemaps/TilemapLayerBase.js#L99
Since: 3.50.0


originX

originX: number

Description:

The horizontal origin of this Tilemap Layer.

Overrides: Phaser.GameObjects.Components.Origin#originX

Source: src/tilemaps/TilemapLayerBase.js#L168
Since: 3.0.0


originY

originY: number

Description:

The vertical origin of this Tilemap Layer.

Overrides: Phaser.GameObjects.Components.Origin#originY

Source: src/tilemaps/TilemapLayerBase.js#L178
Since: 3.0.0


tilemap

tilemap: Phaser.Tilemaps.Tilemap

Description:

The Tilemap that this layer is a part of.

Source: src/tilemaps/TilemapLayerBase.js#L90
Since: 3.50.0


Inherited Methods

From Phaser.Events.EventEmitter:

From Phaser.GameObjects.Components.Alpha:

From Phaser.GameObjects.Components.BlendMode:

From Phaser.GameObjects.Components.ComputedSize:

From Phaser.GameObjects.Components.Depth:

From Phaser.GameObjects.Components.ElapseTimer:

From Phaser.GameObjects.Components.Filters:

From Phaser.GameObjects.Components.Flip:

From Phaser.GameObjects.Components.GetBounds:

From Phaser.GameObjects.Components.Lighting:

From Phaser.GameObjects.Components.Mask:

From Phaser.GameObjects.Components.Origin:

From Phaser.GameObjects.Components.RenderNodes:

From Phaser.GameObjects.Components.RenderSteps:

From Phaser.GameObjects.Components.ScrollFactor:

From Phaser.GameObjects.Components.Transform:

From Phaser.GameObjects.Components.Visible:

From Phaser.GameObjects.GameObject:

From Phaser.Physics.Arcade.Components.Collision:


Public Methods

calculateFacesAt

<instance> calculateFacesAt(tileX, tileY)

Description:

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.

Parameters:

nametypeoptionaldescription
tileXnumberNoThe x coordinate.
tileYnumberNoThe y coordinate.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L231
Since: 3.50.0


calculateFacesWithin

<instance> calculateFacesWithin([tileX], [tileY], [width], [height])

Description:

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.

Parameters:

nametypeoptionaldescription
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L251
Since: 3.50.0


copy

<instance> copy(srcTileX, srcTileY, width, height, destTileX, destTileY, [recalculateFaces])

Description:

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.

Parameters:

nametypeoptionaldefaultdescription
srcTileXnumberNoThe x coordinate of the area to copy from, in tiles, not pixels.
srcTileYnumberNoThe y coordinate of the area to copy from, in tiles, not pixels.
widthnumberNoThe width of the area to copy, in tiles, not pixels.
heightnumberNoThe height of the area to copy, in tiles, not pixels.
destTileXnumberNoThe x coordinate of the area to copy to, in tiles, not pixels.
destTileYnumberNoThe y coordinate of the area to copy to, in tiles, not pixels.
recalculateFacesbooleanYestruetrue if the faces data should be recalculated.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L298
Since: 3.50.0


createFromTiles

<instance> createFromTiles(indexes, replacements, [spriteConfig], [scene], [camera])

Description:

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:

nametypeoptionaldescription
indexesnumber | arrayNoThe tile index, or array of indexes, to create Sprites from.
replacementsnumber | arrayNoThe 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.
spriteConfigPhaser.Types.GameObjects.Sprite.SpriteConfigYesThe config object to pass into the Sprite creator (i.e. scene.make.sprite).
scenePhaser.SceneYesThe Scene to create the Sprites within.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when determining the world XY

Returns: Array.<Phaser.GameObjects.Sprite> - An array of the Sprites that were created.

Source: src/tilemaps/TilemapLayerBase.js#L273
Since: 3.50.0


destroy

<instance> destroy([removeFromTilemap])

Description:

Destroys this TilemapLayer and removes its link to the associated LayerData.

Parameters:

nametypeoptionaldefaultdescription
removeFromTilemapbooleanYestrueRemove this layer from the parent Tilemap?

Overrides: Phaser.GameObjects.GameObject#destroy

Source: src/tilemaps/TilemapLayerBase.js#L1151
Since: 3.50.0


fill

<instance> fill(index, [tileX], [tileY], [width], [height], [recalculateFaces])

Description:

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.

Parameters:

nametypeoptionaldefaultdescription
indexnumberNoThe tile index to fill the area with.
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.
recalculateFacesbooleanYestruetrue if the faces data should be recalculated.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L323
Since: 3.50.0


filterTiles

<instance> filterTiles(callback, [context], [tileX], [tileY], [width], [height], [filteringOptions])

Description:

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.

Parameters:

nametypeoptionaldescription
callbackfunctionNoThe 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.
contextobjectYesThe context under which the callback should be run.
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area to filter.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area to filter.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.
filteringOptionsPhaser.Types.Tilemaps.FilteringOptionsYesOptional filters to apply when getting the tiles.

Returns: Array.<Phaser.Tilemaps.Tile> - An array of Tile objects.

Source: src/tilemaps/TilemapLayerBase.js#L347
Since: 3.50.0


findByIndex

<instance> findByIndex(index, [skip], [reverse])

Description:

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.

Parameters:

nametypeoptionaldefaultdescription
indexnumberNoThe tile index value to search for.
skipnumberYes0The number of times to skip a matching tile before returning.
reversebooleanYesfalseIf true it will scan the layer in reverse, starting at the bottom-right. Otherwise it scans from the top-left.

Returns: Phaser.Tilemaps.Tile - The first matching Tile object.

Source: src/tilemaps/TilemapLayerBase.js#L372
Since: 3.50.0


findTile

<instance> findTile(callback, [context], [tileX], [tileY], [width], [height], [filteringOptions])

Description:

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.

Parameters:

nametypeoptionaldescription
callbackFindTileCallbackNoThe callback. Each tile in the given area will be passed to this callback as the first and only parameter.
contextobjectYesThe context under which the callback should be run.
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area to search.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area to search.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.
filteringOptionsPhaser.Types.Tilemaps.FilteringOptionsYesOptional filters to apply when getting the tiles.

Returns: Phaser.Tilemaps.Tile - The first Tile found at the given location.

Source: src/tilemaps/TilemapLayerBase.js#L393
Since: 3.50.0


forEachTile

<instance> forEachTile(callback, [context], [tileX], [tileY], [width], [height], [filteringOptions])

Description:

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.

Parameters:

nametypeoptionaldescription
callbackEachTileCallbackNoThe callback. Each tile in the given area will be passed to this callback as the first and only parameter.
contextobjectYesThe context, or scope, under which the callback should be run.
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area to search.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area to search.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.
filteringOptionsPhaser.Types.Tilemaps.FilteringOptionsYesOptional filters to apply when getting the tiles.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L416
Since: 3.50.0


getIsoTileAtWorldXY

<instance> getIsoTileAtWorldXY(worldX, worldY, [originTop], [nonNull], [camera])

Description:

Gets a tile at the given world coordinates from the given isometric layer.

Parameters:

nametypeoptionaldefaultdescription
worldXnumberNoX position to get the tile from (given in pixels)
worldYnumberNoY position to get the tile from (given in pixels)
originTopbooleanYestrueWhich is the active face of the isometric tile? The top (default, true), or the base? (false)
nonNullbooleanYesfalseFor empty tiles, return a Tile object with an index of -1 instead of null.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: Phaser.Tilemaps.Tile - The tile at the given coordinates or null if no tile was found or the coordinates were invalid.

Source: src/tilemaps/TilemapLayerBase.js#L475
Since: 3.60.0


getTileAt

<instance> getTileAt(tileX, tileY, [nonNull])

Description:

Gets a tile at the given tile coordinates from the given layer.

Parameters:

nametypeoptionaldefaultdescription
tileXnumberNoX position to get the tile from (given in tile units, not pixels).
tileYnumberNoY position to get the tile from (given in tile units, not pixels).
nonNullbooleanYesfalseFor empty tiles, return a Tile object with an index of -1 instead of null.

Returns: Phaser.Tilemaps.Tile - The Tile at the given coordinates or null if no tile was found or the coordinates were invalid.

Source: src/tilemaps/TilemapLayerBase.js#L440
Since: 3.50.0


getTileAtWorldXY

<instance> getTileAtWorldXY(worldX, worldY, [nonNull], [camera])

Description:

Gets a tile at the given world coordinates from the given layer.

Parameters:

nametypeoptionaldefaultdescription
worldXnumberNoX position to get the tile from (given in pixels)
worldYnumberNoY position to get the tile from (given in pixels)
nonNullbooleanYesfalseFor empty tiles, return a Tile object with an index of -1 instead of null.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: Phaser.Tilemaps.Tile - The tile at the given coordinates or null if no tile was found or the coordinates were invalid.

Source: src/tilemaps/TilemapLayerBase.js#L457
Since: 3.50.0


getTileCorners

<instance> getTileCorners(tileX, tileY, [camera])

Description:

Returns an array of Vector2s where each entry corresponds to the corner of the requested tile.

The tileX and tileY parameters are in tile coordinates, not world coordinates.

The corner coordinates are in world space, having factored in TilemapLayer scale, position

and the camera, if given.

The size of the array will vary based on the orientation of the map. For example an

orthographic map will return an array of 4 vectors, where-as a hexagonal map will,

of course, return an array of 6 corner vectors.

Parameters:

nametypeoptionaldescription
tileXnumberNoThe x coordinate, in tiles, not pixels.
tileYnumberNoThe y coordinate, in tiles, not pixels.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: Array.<Phaser.Math.Vector2> - Returns an array of Vector2s, or null if the layer given was invalid.

Source: src/tilemaps/TilemapLayerBase.js#L1027
Since: 3.60.0


getTilesWithin

<instance> getTilesWithin([tileX], [tileY], [width], [height], [filteringOptions])

Description:

Gets the tiles in the given rectangular area (in tile coordinates) of the layer.

Parameters:

nametypeoptionaldescription
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.
filteringOptionsPhaser.Types.Tilemaps.FilteringOptionsYesOptional filters to apply when getting the tiles.

Returns: Array.<Phaser.Tilemaps.Tile> - An array of Tile objects found within the area.

Source: src/tilemaps/TilemapLayerBase.js#L500
Since: 3.50.0


getTilesWithinShape

<instance> getTilesWithinShape(shape, [filteringOptions], [camera])

Description:

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.

Parameters:

nametypeoptionaldescription
shapePhaser.Geom.Circle | Phaser.Geom.LinePhaser.Geom.RectanglePhaser.Geom.Triangle
filteringOptionsPhaser.Types.Tilemaps.FilteringOptionsYesOptional filters to apply when getting the tiles.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when factoring in which tiles to return.

Returns: Array.<Phaser.Tilemaps.Tile> - An array of Tile objects found within the shape.

Source: src/tilemaps/TilemapLayerBase.js#L519
Since: 3.50.0


getTilesWithinWorldXY

<instance> getTilesWithinWorldXY(worldX, worldY, width, height, [filteringOptions], [camera])

Description:

Gets the tiles in the given rectangular area (in world coordinates) of the layer.

Parameters:

nametypeoptionaldescription
worldXnumberNoThe world x coordinate for the top-left of the area.
worldYnumberNoThe world y coordinate for the top-left of the area.
widthnumberNoThe width of the area.
heightnumberNoThe height of the area.
filteringOptionsPhaser.Types.Tilemaps.FilteringOptionsYesOptional filters to apply when getting the tiles.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when factoring in which tiles to return.

Returns: Array.<Phaser.Tilemaps.Tile> - An array of Tile objects found within the area.

Source: src/tilemaps/TilemapLayerBase.js#L537
Since: 3.50.0


hasTileAt

<instance> hasTileAt(tileX, tileY)

Description:

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.

Parameters:

nametypeoptionaldescription
tileXnumberNoThe x coordinate, in tiles, not pixels.
tileYnumberNoThe y coordinate, in tiles, not pixels.

Returns: boolean - true if a tile was found at the given location, otherwise false.

Source: src/tilemaps/TilemapLayerBase.js#L557
Since: 3.50.0


hasTileAtWorldXY

<instance> hasTileAtWorldXY(worldX, worldY, [camera])

Description:

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.

Parameters:

nametypeoptionaldescription
worldXnumberNoThe x coordinate, in pixels.
worldYnumberNoThe y coordinate, in pixels.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when factoring in which tiles to return.

Returns: boolean - true if a tile was found at the given location, otherwise false.

Source: src/tilemaps/TilemapLayerBase.js#L574
Since: 3.50.0


putTileAt

<instance> putTileAt(tile, tileX, tileY, [recalculateFaces])

Description:

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.

Parameters:

nametypeoptionaldefaultdescription
tilenumber | Phaser.Tilemaps.TileNoThe index of this tile to set or a Tile object.
tileXnumberNoThe x coordinate, in tiles, not pixels.
tileYnumberNoThe y coordinate, in tiles, not pixels.
recalculateFacesbooleanYestruetrue if the faces data should be recalculated.

Returns: Phaser.Tilemaps.Tile - The Tile object that was inserted at the given coordinates.

Source: src/tilemaps/TilemapLayerBase.js#L592
Since: 3.50.0


putTileAtWorldXY

<instance> putTileAtWorldXY(tile, worldX, worldY, [recalculateFaces], [camera])

Description:

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.

Parameters:

nametypeoptionaldescription
tilenumber | Phaser.Tilemaps.TileNoThe index of this tile to set or a Tile object.
worldXnumberNoThe x coordinate, in pixels.
worldYnumberNoThe y coordinate, in pixels.
recalculateFacesbooleanYestrue if the faces data should be recalculated.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: Phaser.Tilemaps.Tile - The Tile object that was inserted at the given coordinates.

Source: src/tilemaps/TilemapLayerBase.js#L613
Since: 3.50.0


putTilesAt

<instance> putTilesAt(tile, tileX, tileY, [recalculateFaces])

Description:

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.

Parameters:

nametypeoptionaldefaultdescription
tileArray.<number> | Array.<Array.<number>>Array.<Phaser.Tilemaps.Tile>Array.<Array.<Phaser.Tilemaps.Tile>>No
tileXnumberNoThe x coordinate, in tiles, not pixels.
tileYnumberNoThe y coordinate, in tiles, not pixels.
recalculateFacesbooleanYestruetrue if the faces data should be recalculated.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L635
Since: 3.50.0


randomize

<instance> randomize([tileX], [tileY], [width], [height], [indexes])

Description:

Randomizes the indexes of a rectangular region of tiles (in tile coordinates) within the

specified layer. Each tile will receive 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.

Parameters:

nametypeoptionaldescription
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.
indexesArray.<number>YesAn array of indexes to randomly draw from during randomization.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L659
Since: 3.50.0


removeTileAt

<instance> removeTileAt(tileX, tileY, [replaceWithNull], [recalculateFaces])

Description:

Removes the tile at the given tile coordinates in the specified layer and updates the layers

collision information.

Parameters:

nametypeoptionaldefaultdescription
tileXnumberNoThe x coordinate, in tiles, not pixels.
tileYnumberNoThe y coordinate, in tiles, not pixels.
replaceWithNullbooleanYestrueIf true, this will replace the tile at the specified location with null instead of a Tile with an index of -1.
recalculateFacesbooleanYestruetrue if the faces data should be recalculated.

Returns: Phaser.Tilemaps.Tile - A Tile object.

Source: src/tilemaps/TilemapLayerBase.js#L684
Since: 3.50.0


removeTileAtWorldXY

<instance> removeTileAtWorldXY(worldX, worldY, [replaceWithNull], [recalculateFaces], [camera])

Description:

Removes the tile at the given world coordinates in the specified layer and updates the layers

collision information.

Parameters:

nametypeoptionaldefaultdescription
worldXnumberNoThe x coordinate, in pixels.
worldYnumberNoThe y coordinate, in pixels.
replaceWithNullbooleanYestrueIf true, this will replace the tile at the specified location with null instead of a Tile with an index of -1.
recalculateFacesbooleanYestruetrue if the faces data should be recalculated.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: Phaser.Tilemaps.Tile - The Tile object that was removed from the given location.

Source: src/tilemaps/TilemapLayerBase.js#L703
Since: 3.50.0


renderDebug

<instance> renderDebug(graphics, [styleConfig])

Description:

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.

Parameters:

nametypeoptionaldescription
graphicsPhaser.GameObjects.GraphicsNoThe target Graphics object to draw upon.
styleConfigPhaser.Types.Tilemaps.StyleConfigYesAn object specifying the colors to use for the debug drawing.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L723
Since: 3.50.0


replaceByIndex

<instance> replaceByIndex(findIndex, newIndex, [tileX], [tileY], [width], [height])

Description:

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.

Parameters:

nametypeoptionaldescription
findIndexnumberNoThe index of the tile to search for.
newIndexnumberNoThe index of the tile to replace it with.
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L744
Since: 3.50.0


setCollision

<instance> setCollision(indexes, [collides], [recalculateFaces], [updateLayer])

Description:

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).

Parameters:

nametypeoptionaldefaultdescription
indexesnumber | arrayNoEither a single tile index, or an array of tile indexes.
collidesbooleanYestrueIf true it will enable collision. If false it will clear collision.
recalculateFacesbooleanYestrueWhether or not to recalculate the tile faces after the update.
updateLayerbooleanYestrueIf true, updates the current tiles on the layer. Set to false if no tiles have been placed for significant performance boost.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L768
Since: 3.50.0


setCollisionBetween

<instance> setCollisionBetween(start, stop, [collides], [recalculateFaces])

Description:

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).

Parameters:

nametypeoptionaldefaultdescription
startnumberNoThe first index of the tile to be set for collision.
stopnumberNoThe last index of the tile to be set for collision.
collidesbooleanYestrueIf true it will enable collision. If false it will clear collision.
recalculateFacesbooleanYestrueWhether or not to recalculate the tile faces after the update.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L790
Since: 3.50.0


setCollisionByExclusion

<instance> setCollisionByExclusion(indexes, [collides], [recalculateFaces])

Description:

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). Tile indexes not currently in the layer are not affected.

Parameters:

nametypeoptionaldefaultdescription
indexesArray.<number>NoAn array of the tile indexes to not be counted for collision.
collidesbooleanYestrueIf true it will enable collision. If false it will clear collision.
recalculateFacesbooleanYestrueWhether or not to recalculate the tile faces after the update.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L838
Since: 3.50.0


setCollisionByProperty

<instance> setCollisionByProperty(properties, [collides], [recalculateFaces])

Description:

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.

Parameters:

nametypeoptionaldefaultdescription
propertiesobjectNoAn object with tile properties and corresponding values that should be checked.
collidesbooleanYestrueIf true it will enable collision. If false it will clear collision.
recalculateFacesbooleanYestrueWhether or not to recalculate the tile faces after the update.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L813
Since: 3.50.0


setCollisionFromCollisionGroup

<instance> setCollisionFromCollisionGroup([collides], [recalculateFaces])

Description:

Sets collision on the tiles within a layer by checking each tiles collision group data

(typically defined in Tiled within the tileset collision editor). If any objects are found within

a tiles collision group, the tile's colliding information will be set. The collides parameter

controls if collision will be enabled (true) or disabled (false).

Parameters:

nametypeoptionaldefaultdescription
collidesbooleanYestrueIf true it will enable collision. If false it will clear collision.
recalculateFacesbooleanYestrueWhether or not to recalculate the tile faces after the update.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L859
Since: 3.50.0


setTileIndexCallback

<instance> setTileIndexCallback(indexes, callback, callbackContext)

Description:

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:

nametypeoptionaldescription
indexesnumber | Array.<number>NoEither a single tile index, or an array of tile indexes to have a collision callback set for.
callbackfunctionNoThe callback that will be invoked when the tile is collided with.
callbackContextobjectNoThe context under which the callback is called.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L880
Since: 3.50.0


setTileLocationCallback

<instance> setTileLocationCallback([tileX], [tileY], [width], [height], [callback], [callbackContext])

Description:

Sets a collision callback for the given rectangular area (in tile coordinates) 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.

Parameters:

nametypeoptionaldescription
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.
callbackfunctionYesThe callback that will be invoked when the tile is collided with.
callbackContextobjectYesThe context, or scope, under which the callback is invoked.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L902
Since: 3.50.0


shuffle

<instance> shuffle([tileX], [tileY], [width], [height])

Description:

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.

Parameters:

nametypeoptionaldescription
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L926
Since: 3.50.0


swapByIndex

<instance> swapByIndex(tileA, tileB, [tileX], [tileY], [width], [height])

Description:

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.

Parameters:

nametypeoptionaldescription
tileAnumberNoFirst tile index.
tileBnumberNoSecond tile index.
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L949
Since: 3.50.0


tileToWorldX

<instance> tileToWorldX(tileX, [camera])

Description:

Converts from tile X coordinates (tile units) to world X coordinates (pixels), factoring in the

layers position, scale and scroll.

Parameters:

nametypeoptionaldescription
tileXnumberNoThe x coordinate, in tiles, not pixels.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: number - The Tile X coordinate converted to pixels.

Source: src/tilemaps/TilemapLayerBase.js#L973
Since: 3.50.0


tileToWorldXY

<instance> tileToWorldXY(tileX, tileY, [point], [camera])

Description:

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.

Parameters:

nametypeoptionaldescription
tileXnumberNoThe x coordinate, in tiles, not pixels.
tileYnumberNoThe y coordinate, in tiles, not pixels.
pointPhaser.Math.Vector2YesA Vector2 to store the coordinates in. If not given a new Vector2 is created.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: Phaser.Math.Vector2 - A Vector2 containing the world coordinates of the Tile.

Source: src/tilemaps/TilemapLayerBase.js#L1007
Since: 3.50.0


tileToWorldY

<instance> tileToWorldY(tileY, [camera])

Description:

Converts from tile Y coordinates (tile units) to world Y coordinates (pixels), factoring in the

layers position, scale and scroll.

Parameters:

nametypeoptionaldescription
tileYnumberNoThe y coordinate, in tiles, not pixels.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: number - The Tile Y coordinate converted to pixels.

Source: src/tilemaps/TilemapLayerBase.js#L990
Since: 3.50.0


weightedRandomize

<instance> weightedRandomize(weightedIndexes, [tileX], [tileY], [width], [height])

Description:

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.

Parameters:

nametypeoptionaldescription
weightedIndexesArray.<object>NoAn 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.
tileXnumberYesThe left most tile index (in tile coordinates) to use as the origin of the area.
tileYnumberYesThe top most tile index (in tile coordinates) to use as the origin of the area.
widthnumberYesHow many tiles wide from the tileX index the area will be.
heightnumberYesHow many tiles tall from the tileY index the area will be.

Returns: Phaser.Tilemaps.TilemapLayerBase - This Tilemap Layer object.

Source: src/tilemaps/TilemapLayerBase.js#L1053
Since: 3.50.0


worldToTileX

<instance> worldToTileX(worldX, [snapToFloor], [camera])

Description:

Converts from world X coordinates (pixels) to tile X coordinates (tile units), factoring in the

layers position, scale and scroll.

You cannot call this method for Isometric or Hexagonal tilemaps as they require

both worldX and worldY values to determine the correct tile, instead you

should use the worldToTileXY method.

Parameters:

nametypeoptionaldescription
worldXnumberNoThe x coordinate to be converted, in pixels, not tiles.
snapToFloorbooleanYesWhether or not to round the tile coordinate down to the nearest integer.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: number - The tile X coordinate based on the world value.

Source: src/tilemaps/TilemapLayerBase.js#L1086
Since: 3.50.0


worldToTileXY

<instance> worldToTileXY(worldX, worldY, [snapToFloor], [point], [camera])

Description:

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.

Parameters:

nametypeoptionaldescription
worldXnumberNoThe x coordinate to be converted, in pixels, not tiles.
worldYnumberNoThe y coordinate to be converted, in pixels, not tiles.
snapToFloorbooleanYesWhether or not to round the tile coordinate down to the nearest integer.
pointPhaser.Math.Vector2YesA Vector2 to store the coordinates in. If not given a new Vector2 is created.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: Phaser.Math.Vector2 - A Vector2 containing the tile coordinates of the world values.

Source: src/tilemaps/TilemapLayerBase.js#L1130
Since: 3.50.0


worldToTileY

<instance> worldToTileY(worldY, [snapToFloor], [camera])

Description:

Converts from world Y coordinates (pixels) to tile Y coordinates (tile units), factoring in the

layers position, scale and scroll.

You cannot call this method for Isometric or Hexagonal tilemaps as they require

both worldX and worldY values to determine the correct tile, instead you

should use the worldToTileXY method.

Parameters:

nametypeoptionaldescription
worldYnumberNoThe y coordinate to be converted, in pixels, not tiles.
snapToFloorbooleanYesWhether or not to round the tile coordinate down to the nearest integer.
cameraPhaser.Cameras.Scene2D.CameraYesThe Camera to use when calculating the tile index from the world values.

Returns: number - The tile Y coordinate based on the world value.

Source: src/tilemaps/TilemapLayerBase.js#L1108
Since: 3.50.0