Tilemaps are a fundamental tool in 2D game development, allowing you to build large, complex game worlds efficiently by reusing small images called tiles. In this section, you’ll learn what tilemaps are, how to create and load them in Phaser, and how to use them to construct your own game worlds.


  1. What is a Tilemap?

A tilemap is a grid-based layout where each cell (tile) references a small image (tile graphic) from a larger image called a tileset. By arranging these tiles, you can create levels, backgrounds, and environments for your game.

Key Concepts:

  • Tile: A small, reusable image (e.g., 32x32 pixels).
  • Tileset: A single image containing all the tiles.
  • Tilemap: A data structure (often a JSON file) describing how tiles are arranged in the game world.
Term Description Example
Tile Single image used in the map Grass, water, wall
Tileset Image containing multiple tiles tileset.png
Tilemap Data describing tile arrangement (JSON/CSV) map.json

  1. Creating a Tilemap

Step 1: Designing Your Tilemap

You can design tilemaps using tools like Tiled Map Editor. Export your map as a JSON file and your tileset as a PNG image.

Example Files:

  • map.json (tilemap data)
  • tileset.png (tileset image)

Step 2: Loading Tilemap Assets in Phaser

You need to load both the tilemap and the tileset image in your Phaser scene’s preload method.

function preload() {
  // Load the tilemap JSON
  this.load.tilemapTiledJSON('map', 'assets/map.json');
  // Load the tileset image
  this.load.image('tiles', 'assets/tileset.png');
}

Explanation:

  • tilemapTiledJSON loads a tilemap exported from Tiled in JSON format.
  • load.image loads the tileset image.

  1. Creating the Tilemap in the Scene

After loading, you can create the tilemap and add layers in the create method.

function create() {
  // Create the tilemap
  const map = this.make.tilemap({ key: 'map' });

  // Add the tileset image to the map
  const tileset = map.addTilesetImage('tileset', 'tiles');

  // Create a layer from the tilemap
  const groundLayer = map.createLayer('Ground', tileset, 0, 0);
}

Explanation:

  • this.make.tilemap({ key: 'map' }): Creates a tilemap using the loaded JSON.
  • addTilesetImage('tileset', 'tiles'): Links the tileset name in Tiled (tileset) to the loaded image (tiles).
  • createLayer('Ground', tileset, 0, 0): Creates a visible layer named "Ground" at position (0, 0).

  1. Practical Example: Complete Scene

Here’s a minimal example of a Phaser scene that loads and displays a tilemap:

class MyGameScene extends Phaser.Scene {
  preload() {
    this.load.tilemapTiledJSON('map', 'assets/map.json');
    this.load.image('tiles', 'assets/tileset.png');
  }

  create() {
    const map = this.make.tilemap({ key: 'map' });
    const tileset = map.addTilesetImage('tileset', 'tiles');
    const groundLayer = map.createLayer('Ground', tileset, 0, 0);
  }
}

const config = {
  type: Phaser.AUTO,
  width: 800,
  height: 600,
  scene: MyGameScene
};

const game = new Phaser.Game(config);

Explanation:

  • The scene loads the tilemap and tileset, then creates and displays the "Ground" layer.

  1. Exercise: Load and Display a Tilemap

Task:
Download a simple tilemap and tileset (or create your own using Tiled). Load and display the tilemap in a Phaser scene.

Steps:

  1. Create a new Phaser scene.
  2. Load the tilemap JSON and tileset image in preload.
  3. Create the tilemap and add a layer in create.

Solution:

class TilemapScene extends Phaser.Scene {
  preload() {
    this.load.tilemapTiledJSON('map', 'assets/map.json');
    this.load.image('tiles', 'assets/tileset.png');
  }

  create() {
    const map = this.make.tilemap({ key: 'map' });
    const tileset = map.addTilesetImage('tileset', 'tiles');
    const layer = map.createLayer('Ground', tileset, 0, 0);
  }
}

const config = {
  type: Phaser.AUTO,
  width: 640,
  height: 480,
  scene: TilemapScene
};

const game = new Phaser.Game(config);

Common Mistakes & Tips:

  • Tileset Name Mismatch: Ensure the name in addTilesetImage matches the name used in Tiled.
  • File Paths: Double-check asset paths (assets/map.json, assets/tileset.png).
  • Layer Name: The layer name in createLayer must match the one defined in your tilemap.

  1. Summary

  • Tilemaps allow you to build large, efficient game worlds by reusing small images (tiles).
  • Use tools like Tiled to design your maps and export them as JSON.
  • In Phaser, load the tilemap and tileset, then create layers to display your world.
  • Pay attention to naming and file paths to avoid common errors.

Next: In the following section, you’ll learn how to implement scrolling and camera control to navigate your game world!

© Copyright 2024. All rights reserved