Home Guides

How to make a Phaser 3 tilemap

Paint your level in TileMap Studio, export it as a JavaScript module, import it into your scene and pass the tile grid to this.make.tilemap(). If you prefer Phaser's Tiled loader, export a .tmx, open it in Tiled and save it as JSON.

Updated

1. Export the map as a JavaScript module

In TileMap Studio choose File → Export → JavaScript module. You get a file like this:

// Generated by TileMap Studio
export const level = {
  "width": 40, "height": 24, "tileSize": 16,
  "tilesets": [{ "name": "RPG Urban (demo)", "firstgid": 1, "tileWidth": 16, "tileHeight": 16, "columns": 27 }],
  "layers": [{ "name": "Ground", "visible": true, "opacity": 1, "gids": [[1, 1, 2, …], …] }]
};

Each layer's gids is a grid of tile numbers: 0 is empty, and tiles from the first tileset start at its firstgid.

2. Load it in a scene

import { level } from './level1.js';

export class Play extends Phaser.Scene {
  preload() {
    this.load.image('tiles', 'assets/rpg-urban-pack.png');
  }

  create() {
    const { firstgid } = level.tilesets[0];
    const map = this.make.tilemap({ tileWidth: level.tileSize, tileHeight: level.tileSize, width: level.width, height: level.height });
    const tileset = map.addTilesetImage('tiles');

    for (const l of level.layers) {
      const data = l.gids.map((row) => row.map((g) => (g ? g - firstgid : -1)));
      const layer = map.createBlankLayer(l.name, tileset);
      layer.putTilesAt(data, 0, 0);
      layer.setVisible(l.visible).setAlpha(l.opacity);
    }
  }
}

Every TileMap Studio layer becomes a Phaser layer, in the same order, with visibility and opacity kept. For a single-layer map you can also pass the grid straight in: this.make.tilemap({ data, tileWidth, tileHeight }).

3. Collisions and the camera

const walls = map.getLayer('Walls').tilemapLayer;
walls.setCollisionByExclusion([-1]);
this.physics.add.collider(player, walls);
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels).startFollow(player);

Paint walls and obstacles on their own layer in the editor (for example "Walls") and every tile on it becomes solid.

Using Phaser's Tiled loader instead

If your project already uses this.load.tilemapTiledJSON(), export a .tmx, open it in Tiled and choose File → Export As → JSON map. Make sure the tileset is embedded in the map, not saved as a separate .tsx file, because Phaser can't read external tilesets.

Pixel art settings

Set pixelArt: true in your game config so Phaser uses nearest-neighbour scaling, and keep camera zoom at whole numbers (2, 3, 4).

Questions

Does Phaser 3 load .tmx files?

No. Phaser loads Tiled maps saved as JSON (this.load.tilemapTiledJSON). Open TileMap Studio's .tmx export in Tiled and choose File, Export As, JSON, or skip Tiled and use the JavaScript module export.

Why subtract the firstgid?

Map files number tiles from 1, with 0 meaning empty. Phaser's data tilemaps index tiles from 0 and use -1 for empty, so each GID becomes gid - firstgid, and 0 becomes -1.

Can I add collisions?

Yes. After creating the layer, call layer.setCollision([...indexes]) or layer.setCollisionByExclusion([-1]) to make every placed tile solid, then add a collider between the layer and your player.

Paint your Phaser level

Open the free editor