-
Notifications
You must be signed in to change notification settings - Fork 4
Tilemaps
samme edited this page Aug 7, 2023
·
32 revisions
- Modular Game Worlds in Phaser 3 — Static Maps
- Modular Game Worlds in Phaser 3 — Dynamic Platformer
- Modular Game Worlds in Phaser 3 — Procedural Dungeon
The basic procedure is:
- Load a tileset image
- Load a tilemap file, if using one
- Create a tilemap object
- Assign tileset images to the tilemap
- Create a tilemap layer, specifying one or more tilesets for it
Every tilemap layer needs at least one tileset image to render. You assign this to the map with addTilesetImage().
load.image() is fine for this, but you can also use load.spritesheet() (with the appropriate frame dimensions) if you want to use the tileset image as a multiframe texture for other game objects like sprites.
If you get any errors loading images, fix those first before creating the tilemap.
- Tiled JSON map example
- Tiled multiple tilesets example
- Static Maps: Building a Map in Tiled etc.
The Tiled JSON map already has the tile and map dimensions, so you usually don't override those. Just give the map key that you loaded:
const tilemap = this.add.tilemap('map');
If you've forgotten the tile layer or tileset names, they're all in there:
console.log('tile layers', tilemap.layers);
console.log('tilesets', tilemap.tilesets);
const map = this.add.tilemap(/*…*/);
const [firstObjectLayer] = map.objects;
console.info('Object properties in %s:', firstObjectLayer.name);
console.table(firstObjectLayer.objects);
console.info('Object custom properties in %s:', firstObjectLayer.name);
console.table(firstObjectLayer.objects.map(o => o.properties));