Using Tiled editor to create levels and import them as json data in your HTML5 swipe controlled Sokoban game made with Phaser

Apparently, the game I am about to show you now is the same you can see in the post HTML5 swipe controlled Sokoban game made with Phaser featuring unlimited undos – solving and advancing through levels and actually is the same game, or at least it works in the same way:

Swipe to move the player, double click/tap to undo last move.

The difference is this time I didn’t hardcode the levels but I created them with Tiled Map Editor. I really encourage you to use Tiled to design levels for your games, no matter if tile-based games like Sokoban or platformers, shooters and so on, as long as they are 2D games, even isometric.

Let’s see how to create and import levels made with Tiled into your game.

First, we create a new file, with these settings:

We are using a 8×8 tiles orthogonal map, where each tile will be a 40 pixels square. It’s not important the size of the tile, it’s just our Sokoban spritesheet has 40×40 assets and we will use the same spritesheet both for the game and for the editor.

Then it’s time to load a tileset, press “new tileset” and load the sokoban tiles. Be sure you are loading a 40×40 tileset, as this is the size you declared before.

And that’s it!! Your tiles have been loaded.

Now you can paint your first level, here it is:

Once you made your first level, create a new layer and proceed building the other levels. I am making only two levels but obviously there’s no limit.

This is my second level. Also noticed I gave each layer the level name. It’s not necessary, but as it’s part of the information you will export, keep in mind you can name your layers.

When it’s time to export the level, save it as a JSON file, I called it map.json and put it directly in game folder.

The exported file will look like this:

Now we are ready to load level information and build level array with a simple asynchronous request we are making in a preloader state. Here is the source code, with new lines highlighted. Check the previous step to see what changed.

Using a tool like Tiled will allow you to speed up level design process. Download the source code.

  • But the game.load object of phaser can load .json files instead

  • Scott Hatfield

    What’s the reason for case PLAYER + SPOT and CRATE + SPOT in the drawLevel function?

    • Scott Hatfield

      Nevermind – I hadn’t noticed the last two frames on your spritesheet. :)