Platformer Tutorial with Phaser and Tiled – Part 2

In my last tutorial, we built a platformer game using Phaser and Tiled. In the following two tutorials we will add content to this game, making it more complete. In this tutorial, the following content will be added:

  • Bigger levels, and two of them, so we can use the multilevel feature we implemented in the last tutorial.
  • Smarter enemies, that don’t fall from platforms.
  • A new enemy, which follows the player when you get close to it.
    Player score, which will be increased when the player kill enemies or collect coins.
  • A checkpoint, so if the player dies after reach it, it will be respawn at the checkpoint position.

For this tutorial, it is necessary that you finished the last tutorial, since we will continue from the code we already wrote.

Learn Phaser by building 15 games

If you want to master Phaser and learn how to publish Phaser games as native games for iOS and Android feel free to check Zenva‘s online course The Complete Mobile Game Development Course – Build 15 Games.

Source code files

You can download the tutorial source code files here.

Game States

In this tutorial, we will use the same states from the last one:

  • Boot State: loads a json file with the level information and starts the Loading State.
  • Loading Sate: loads all the game assets, and starts the Level State.
  • Tiled State: creates the map and all game objects.

The code for Boot and Loading states are exactly the same as before, so I will omit them. On the other hand, the Tiled State code had some changes. Some of those changes are necessary because of the new content added to the game and will be explained throughout the tutorial. However, there is one change that was made to make the code simpler, and I will explain it now.

In the previous tutorial, we had a “create_object” method that used a switch case to instantiate the correct prefab. To avoid having to add another case condition for each prefab we create, we will change it to use an object that maps each prefab type to its constructor, as shown below:

Notice that, in the TiledState constructor we create an object with all prefab types and their corresponding constructors. So, in the “create_object” method we can call the correct constructor using the type property from the map object. This is only possible because all prefabs use the same constructor. So, if you created your prefabs with different constructors you can change them to have the same constructor, or keep using the switch case strategy, as in the previous tutorial. Remember that all code I show here is based on my preference, and you should write it the way you think it’s best.

New levels

Those are the two levels I created for this tutorial. Feel free to create your owns, trying to add different tiles and enemies. In those new maps, there are already the new prefabs I will explain later in the tutorial, there is, however, something I would like to show first.

level1 level2

In the first level, I added a bridge between two platforms, as you can see in the image. This bridge sprite is smaller than the tile, and I wanted its collision box to be smaller too. So, I had to add a new tileset using the same tileset image but different tile sizes. The tile size used was 35 pixels width and 11 pixels height with 1 pixel margin and spacing (as shown below). It is also possible to insert the bridge as an object, not a tile, but I found this way easier.


Also, notice that I divided the background layer in two: sky and background. This was done to add some background decoration, like the signs at the beginning and end of the level.

Smarter enemy

We will change our enemy so it will switch direction if arrives the edge of platform. The idea is to check if the next ground position is occupied by a tile and, if not so, switch the enemy direction. To find this next ground position we have to calculate its x and y coordinates. The y coordinate is just the enemy bottom y plus 1, since that’s the y coordinate immediately below the enemy. The x coordinate depends on the enemy direction. So, it will be the enemy x coordinate plus tile width, if the enemy is moving right, or minus tile width if the enemy is moving left.

To check if a position is occupied by a tile, we will use the tilemap method “getTileWordlXY”, which returns the tile object of a layer in a given position (for more information, check Phaser documentation: If the method returns a tile, we know the enemy is not in the platform edge yet, so it can keep moving. If it returns null, the enemy must switch direction to avoid falling. In this code, the method “switch_direction” is the code we already had in the Enemy prefab, I just moved it to a function (you can check it in the source code).

New enemy

Also, we will add an enemy with the following behavior:

  • If the player is outside a detection range, it will act like a regular enemy.
  • If the player is inside a detection range, it will increase its velocity and go in the player direction.

To do that, we will create a new prefab that extends the GroundEnemy prefab. Besides all the regular enemy properties, it will have a detection distance and a running speed. In the update method, we check if the player is inside the detection range. If so, the enemy change its velocity to the running speed and follows the player. Otherwise, it just calls the GroundEnemy update method.

To check if the player is inside the detection range, we calculate the distance between the player and enemy x positions. If the absolute value of this distance is less than the detection distance and both the player and enemy bottom y positions are the same (meaning they’re standing in the same ground), the enemy detects the player.

Player score

The player score will increase every time it kills an enemy or collect a coin. For this, we have to add a score property to the Enemy prefab and create a Coin prefab, as follows.

For the enemy killing, we just have to increase the player score in the “hit_enemy” method, while for collecting coins we check for overlaps between the player and the coins, and increase the player score according to the coin score. To keep track of the player score, we will add a score property in the Player as well.

To show the score in the screen, we create a Score prefab, that extends Phaser.Text instead of Phaser.Sprite. Notice that Phaser.Text extends Phaser.Sprite too, so our prefab still is a Phaser sprite. This Score prefab must be fixed to the camera, so it won’t move with the player and in the update method it will change its text to be equivalent to the player current score.

Here is an example of the score in our game. Try it yourself!



Our checkpoint will be simple, if the player dies after touching the checkpoint, it will be respawned in the checkpoint position instead of its initial position. For this, we will add a checkpoint prefab that checks for overlap with the player, and if that happens, set a checkpoint variable to true. So, in the “restart_level” method from TiledState (presented above) we now check if the checkpoint was reached. If so, we just respawn the player to the checkpoint position, instead of restarting the level.

Here is an example of the checkpoint in our game. Try it yourself!


Finishing the game

Now that we have all the game code, we just have to add the prefab properties in our Tiled map, as we did in the last tutorial, and work game is working!

Show me the levels you created, and tell me your opinion about this tutorial. In the next tutorial, we will add lives to our player, some items and a boss level, so tell me if you have any suggestion, too!

Published by

Renan Oliveira

Renan is a computer science master student and game enthusiast. His interest in game development started a few years ago with a 2D game engine course, which resulted in a small 2D engine and game. He started working with Javascript and Phaser with the Zenva Game Development Course. Currently, he is working in his own game.

Share this article

  • Hervé Touitou


    Bravo for your tutorial.

    I would like to add sound but I do not see how …

    Thank you