phaserjs/phaser/tilemaps

Claude Code and agent skill by phaserjs/phaser

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Tile maps in Phaser 4: load.tilemapTiledJSON and load.tilemapCSV, make.tilemap, addTilesetImage, createLayer and createBlankLayer, TilemapGPULayer, getTileAt, putTileAt, removeTileAt, getTilesWithinShape, tile collision with setCollisionByProperty and setCollision, Arcade colliders, object layers with createFromObjects, isometric, hexagonal and staggered maps, and layer culling. Use when building a level, platformer or dungeon map, a procedural or editable map, or placing enemies and pickups from Tiled. Corrects: Tiled tile IDs are not tile indexes, an overlap with a layer includes empty tiles, putTileAt keeps the old tile's properties, and the GPU layer is WebGL, orthogonal and single-tileset. Triggers on: tilemap, Tiled, tileset, tile layer, tile collision, object layer.

Install

npx vskill@latest install phaserjs/phaser/tilemaps

SKILL.md

The same SKILL.md was published earlier as phaserjs/phaser/tilemaps.

Tilemaps

A Tilemap holds the parsed map data and is not drawn. Its layers, TilemapLayer (CPU, every renderer and orientation) or TilemapGPULayer (WebGL, orthogonal), are the Game Objects that draw tiles. Most tile methods live on TilemapLayerBase, shared by both.

Source: src/tilemaps/Tilemap.js, src/tilemaps/TilemapLayerBase.js, src/tilemaps/TilemapLayer.js, src/tilemaps/TilemapGPULayer.js, src/tilemaps/Tile.js, src/tilemaps/Tileset.js, src/tilemaps/components/, src/tilemaps/parsers/tiled/. Related: physics-arcade, physics-matter, loading-assets, cameras.

Quick start

A platformer level from Tiled, with Arcade Physics enabled in the game config.

js
class Level extends Phaser.Scene {
preload() {
this.load.tilemapTiledJSON('level1', 'assets/level1.json');
this.load.image('tiles', 'assets/tiles.png');
this.load.image('player', 'assets/player.png');
}
create() {
const map = this.make.tilemap({ key: 'level1' });
const tileset = map.addTilesetImage('terrain', 'tiles'); // tileset name in Tiled, texture key
const ground = map.createLayer('Ground', tileset); // layer name in Tiled
ground.setCollisionByProperty({ collides: true }); // a custom bool set in Tiled's tileset editor
this.player = this.physics.add.sprite(100, 100, 'player');
this.physics.add.collider(this.player, ground);
this.physics.world.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
this.cameras.main.startFollow(this.player);
}
}

The other snippets continue this one: map, tileset, ground and this.player come from here.

  • this.make.tilemap(config) and this.add.tilemap(key) both return a Tilemap. Neither is on the display list.
  • In Tiled, embed the tilesets in the map. External .tsx tilesets aren't loaded. Compressed layer data (zlib, gzip, zstd) is skipped with a warning, so save layers as CSV or uncompressed Base64. Infinite maps work.

Tilesets and layers

map.addTilesetImage(tilesetName, key, tileWidth, tileHeight, tileMargin, tileSpacing, gid, tileOffset)

  • tilesetName is the tileset's name inside Tiled, not a file name. If it doesn't match, the method returns null and logs the map's tilesets. key defaults to tilesetName.
  • A Tiled map supplies the tile size, margin and spacing. Pass them only to override.
  • The Tileset uses the whole texture image. Atlas frames are ignored, and a multi-atlas texture isn't supported (only its first image is used). Use the DynamicTexture workaround below.
  • Image collection tilesets ("Collection of Images" in Tiled) are parsed. Each image becomes its own Tileset, named by the image path as written in the JSON. Since 4.3 they keep their tile properties, type and collision group.
js
const tree = map.addTilesetImage('../img/tree.png', 'tree'); // name = the image path in the JSON
const props = map.createLayer('Props', [tileset, tree]); // a CPU layer can take several tilesets

map.createLayer(layerID, tileset, x, y, gpu)

  • layerID is the Tiled layer name or its index. Layers inside a Tiled group are named 'Group/Layer'. An unknown name returns null and logs the valid names.
  • Each layer can be created once. A second call returns null.
  • x and y default to the layer's offset in Tiled, not (0, 0).
  • Layers draw in creation order. Use setDepth to put sprites between them.
A tileset from atlas frames

A Tileset needs one image laid out as a grid. To build one from atlas or multi-atlas frames, stamp them into a DynamicTexture in the order the Tiled tileset expects.

js
const size = 32;
const cols = 4;
const names = ['grass', 'dirt', 'water', 'stone', 'sand', 'snow']; // frames of the 'world' atlas
const sheet = this.textures.addDynamicTexture('terrainSheet', cols * size, Math.ceil(names.length / cols) * size);
names.forEach((name, i) => {
sheet.stamp('world', name, (i % cols) * size, Math.floor(i / cols) * size, { originX: 0, originY: 0 });
});
sheet.render(); // v4: nothing is drawn into a DynamicTexture until render()
const terrain = map.addTilesetImage('terrain', 'terrainSheet', size, size);

Maps without Tiled

js
// From a 2D array. -1 is an empty cell. For a non-Tiled map, index 0 is the tileset's first tile.
const level = [
[ -1, -1, -1, -1, -1 ],
[ -1, 4, -1, -1, -1 ],
[ 0, 1, 1, 1, 2 ]
];
const arrayMap = this.make.tilemap({ data: level, tileWidth: 32, tileHeight: 32 });
const tiles = arrayMap.addTilesetImage('tiles'); // the key defaults to the name
const layer = arrayMap.createLayer(0, tiles);
layer.setCollision([0, 1, 2]);
// A blank map for procedural levels. Every cell starts as an empty Tile, index -1.
const proc = this.make.tilemap({ tileWidth: 32, tileHeight: 32, width: 64, height: 24 });
const procTiles = proc.addTilesetImage('tiles');
const world = proc.createBlankLayer('world', procTiles);
world.fill(1, 0, 23, 64, 1); // index, tileX, tileY, width, height
world.randomize(0, 10, 64, 4, [-1, -1, -1, 7]);
world.setCollision([1, 7]);
  • CSV: this.load.tilemapCSV('cave', 'assets/cave.csv'), then this.make.tilemap({ key: 'cave', tileWidth: 32, tileHeight: 32 }) and createLayer(0, ...).
  • insertNull: true in the make.tilemap config stores empty cells as null instead of index -1 Tiles, which saves memory on large sparse maps. putTileAt still works on those cells. getTileAt(x, y, true) returns null for them, and an overlap skips them.

Tiles

  • tile.index is the global tile index. In a Tiled map it is the tile's ID in Tiled plus its tileset's firstgid (usually 1). The ID shown in Tiled's tileset editor is not the index. -1 is an empty cell.
  • tile.x and tile.y are in tiles. tile.pixelX and tile.pixelY are relative to the layer. tile.getCenterX() and tile.getCenterY() are in world space.
  • tile.properties is a plain object, copied from the tileset when the map is parsed.
  • Tiled's flip and rotate flags arrive as tile.rotation (radians) and tile.flipX. A vertical flip in Tiled is rotation = Math.PI with flipX = true, so tile.flipY stays false.
  • tile.tint, tile.tint2 (4.2) and tile.tintMode work on a CPU layer. layer.setTint(color, tileX, tileY, width, height), setTint2 and setTintMode set them on a region. The tint modes are in game-object-components. A GPU layer ignores tile tint, alpha and visibility.
Get, put and remove
js
this.input.on(Phaser.Input.Events.POINTER_DOWN, (pointer) => {
const tile = ground.getTileAtWorldXY(pointer.worldX, pointer.worldY); // Tile or null
if (tile && tile.properties.breakable) {
ground.removeTileAt(tile.x, tile.y);
}
});
const placed = ground.putTileAt(12, 5, 8); // index, tileX, tileY. null outside the layer
if (placed) {
placed.properties = { ...placed.tileset?.getTileProperties(12) }; // putTileAt keeps the old properties
if (placed.properties.collides) {
placed.setCollision(true); // setCollisionByProperty doesn't cover tiles placed later
}
}
  • getTileAt(tileX, tileY, nonNull): nonNull is the third positional argument. true returns an index -1 Tile for an empty cell instead of null. Outside the layer, and for null cells, the result is still null.
  • putTileAt changes the index of the Tile in that cell. It doesn't refresh properties, and the tile collides only if its index was recorded by setCollision, setCollisionBetween or setCollisionByExclusion. Only put indexes that one of the map's tilesets contains: an unknown index throws.
  • removeTileAt(tileX, tileY, replaceWithNull, recalculateFaces): replaceWithNull defaults to true, so the cell becomes null. Pass false to leave an index -1 Tile.
  • TypeScript: since 4.3, getTileAt, getTileAtWorldXY, putTileAt, putTileAtWorldXY, removeTileAt and removeTileAtWorldXY return Tile | null. With strictNullChecks, check the result before using it.
  • Every tile method also exists on the Tilemap with an extra last layer argument, acting on the current layer (map.setLayer) when it's left out. Call them on the layer instead.
  • Regions and searches: getTilesWithin, getTilesWithinWorldXY, filterTiles, findTile, forEachTile. Their filteringOptions are { isNotEmpty, isColliding, hasInterestingFace }. Editing: fill, putTilesAt, randomize, weightedRandomize, replaceByIndex, swapByIndex, copy, shuffle. Coordinates: worldToTileXY, tileToWorldXY. See src/tilemaps/TilemapLayerBase.js.
Tiles inside a shape
js
const blast = new Phaser.Geom.Circle(this.player.x, this.player.y, 96); // world coordinates
ground.getTilesWithinShape(blast, { isNotEmpty: true })
.filter(tile => tile.properties.breakable)
.forEach(tile => ground.removeTileAt(tile.x, tile.y));

getTilesWithinShape(shape, filteringOptions, camera) takes a Circle, Line, Rectangle or Triangle in world coordinates. Since 4.3 it works on isometric layers, testing each tile's diamond. On hexagonal and staggered layers it logs a warning and returns [].

Collision with Arcade Physics

js
ground.setCollisionByProperty({ collides: true }); // tiles whose properties match
ground.setCollision([1, 2, 3]); // by index
ground.setCollisionBetween(1, 50); // by index, inclusive
ground.setCollisionByExclusion([-1]); // every index now in the layer, except these
ground.setCollisionFromCollisionGroup(); // tiles with shapes in Tiled's collision editor
this.physics.add.collider(this.player, ground);
this.physics.add.overlap(this.player, ground, (player, tile) => {
if (tile.properties.spikes) {
player.setTint(0xff0000);
}
});
  • A collider tests only tiles marked as colliding, so mark them first. An overlap tests every tile under the body, colliding or not, including empty ones with an index of -1. Check tile.index or a property in the callback.
  • The callbacks always get (sprite, tile), whichever order you pass them in.
  • Tile callbacks: layer.setTileIndexCallback(indexes, callback, context), layer.setTileLocationCallback(tileX, tileY, width, height, callback, context) and tile.setCollisionCallback(callback, context). They run only while a collider or overlap exists between the body and the layer: for colliding tiles with a collider, and for every tile with an overlap. A tile callback which returns true cancels the collision with that tile.
  • Only the index methods record indexes for tiles placed later. setCollisionByExclusion records only the indexes in the layer when it's called. setCollisionByProperty and setCollisionFromCollisionGroup record nothing, so set tile.setCollision(true) on tiles you place.
  • One-way tile: tile.setCollision(false, false, true, false) (left, right, up, down) collides on the top face only.
  • A layer has collisionCategory and collisionMask like a body, both 1 by default. See physics-arcade.
  • Arcade treats each tile as a box, so tile collision is only correct on orthogonal maps.
  • Matter: call this.matter.world.convertTilemapLayer(ground) after setting collision. See physics-matter.
js
const debug = this.add.graphics().setDepth(100);
ground.renderDebug(debug, { tileColor: null }); // also: collidingTileColor, faceColor (Phaser.Display.Color or null)

renderDebug draws once, for orthogonal maps. Use a Graphics just for it, and clear() it before calling again.

Object layers

This is the start of the file. Read the full SKILL.md on GitHub.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
39,633
Repository forks
7,130
7-day trend score
53

Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 17 May 2026, 6 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Grok Build (Cursor)
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/phaserjs/phaser/tilemaps/badge)](https://verified-skill.com/skills/phaserjs/phaser/tilemaps)

Something wrong with this skill or its page? Tell us.