Class: Tilemap

Creates a new Phaser.Tilemap object. The map can either be populated with data from a Tiled JSON file or from a CSV file.

Tiled is a free software package specifically for creating tile maps, and is available from http://www.mapeditor.org

To do this pass the Cache key as the first parameter. When using Tiled data you need only provide the key.
When using CSV data you must provide the key and the tileWidth and tileHeight parameters.
If creating a blank tilemap to be populated later, you can either specify no parameters at all and then use Tilemap.create or pass the map and tile dimensions here.
Note that all Tilemaps use a base tile size to calculate dimensions from, but that a TilemapLayer may have its own unique tile size that overrides it.
A Tile map is rendered to the display using a TilemapLayer. It is not added to the display list directly itself.
A map may have multiple layers. You can perform operations on the map data such as copying, pasting, filling and shuffling the tiles around.

Parameters:

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 tileset parameter.
You can also pass in a BitmapData which can be used instead of an Image.

tileWidth

number

<optional>

32

The width of the tiles in the Tileset Image. If not given it will default to the map.tileWidth value, if that isn't set then 32.

tileHeight

number

<optional>

32

The height of the tiles in the Tileset Image. If not given it will default to the map.tileHeight value, if that isn't set then 32.

tileMargin

number

<optional>

0

The width of the tiles in the Tileset Image.

tileSpacing

number

<optional>

0

The height of the tiles in the Tileset Image.

gid

number

<optional>

0

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

Creates a Sprite for every object matching the gid argument. You can optionally specify the group that the Sprite will be created in. If none is
given it will be created in the World. All properties from the map data objectgroup are copied across to the Sprite, so you can use this as an easy way to
configure Sprite properties from within the map editor. For example giving an object a property of alpha: 0.5 in the map editor will duplicate that when the
Sprite is created. You could also give it a value like: body.velocity.x: 100 to set it moving automatically.

The gid argument is matched against:

For a tile object, the tile identifier (gid); or

The object's unique ID (id); or

The object's name (a string)

Parameters:

Name

Type

Argument

Default

Description

name

string

The name of the Object Group to create Sprites from.

gid

number
|
string

The object's tile reference (gid), unique ID (id) or name.

key

string

The Game.cache key of the image that this Sprite will use.

frame

number
|
string

<optional>

If the Sprite image contains multiple frames you can specify which one to use here.

exists

boolean

<optional>

true

The default exists state of the Sprite.

autoCull

boolean

<optional>

false

The default autoCull state of the Sprite. Sprites that are autoCulled are culled from the camera if out of its range.

Creates a Sprite for every object matching the given tile indexes in the map data.
You can specify the group that the Sprite will be created in. If none is given it will be created in the World.
You can optional specify if the tile will be replaced with another after the Sprite is 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

tiles

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 -1 to remove the tile. Set to null to make no change (leave the tile as is).

Returns:

The number of Sprites that were created.

Type

integer

createLayer(layer [, width] [, height] [, group])

Creates a new TilemapLayer object. By default TilemapLayers are fixed to the camera.
The layer parameter is important. If you've created your map in Tiled then you can get this by looking in Tiled and looking at the Layer name.
Or you can open the JSON file it exports and look at the layers[].name value. Either way it must match.
If you wish to create a blank layer to put your own tiles on then see Tilemap.createBlankLayer.

Parameters:

Name

Type

Argument

Description

layer

number
|
string

The layer array index value, or if a string is given the layer name, within the map data that this TilemapLayer represents.

width

number

<optional>

The rendered width of the layer, should never be wider than Game.width. If not given it will be set to Game.width.

height

number

<optional>

The rendered height of the layer, should never be wider than Game.height. If not given it will be set to Game.height.

Returns:

destroy()

Removes all layer data from this tile map and nulls the game reference.
Note: You are responsible for destroying any TilemapLayer objects you generated yourself, as Tilemap doesn't keep a reference to them.

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

Searches the entire map layer for the first tile matching the given index, then returns that Phaser.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:

Name

Type

Argument

Default

Description

index

number

The tile index value to search for.

skip

number

<optional>

0

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

reverse

number

<optional>

false

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

Returns:

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

Sets collision on the given tile or tiles. You can pass in either a single numeric index or an array of indexes: [2, 3, 15, 20].
The collides parameter controls if collision will be enabled (true) or disabled (false).

Sets collision on a range of tiles where the tile IDs increment sequentially.
Calling this with a start value of 10 and a stop value of 14 would set collision for tiles 10, 11, 12, 13 and 14.
The collides parameter controls if collision will be enabled (true) or disabled (false).

Parameters:

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.

Return true from the callback to continue separating the tile and colliding object, or false to cancel the collision for the current tile (see Phaser.Physics.Arcade#separateTile).

Parameters:

Name

Type

Argument

Description

indexes

number
|
array

Either a single tile index, or an array of tile indexes to have a collision callback set for.

Sets a global collision callback for the given map location within the layer. This will affect all tiles on this layer found in the given area.
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.

Return true from the callback to continue separating the tile and colliding object, or false to cancel the collision for the current tile (see Phaser.Physics.Arcade#separateTile).

Parameters:

Name

Type

Argument

Description

x

number

X position of the top left of the area to copy (given in tiles, not pixels)

y

number

Y position of the top left of the area to copy (given in tiles, not pixels)