Platform Games in Phaser 3 – Setting Up

Platform games have been a staple in video games since Mario Brothers came along in the early 80’s. Because the framework gives us a lot of code to work with, setting Up Phaser 3 platform games isn’t too difficult. There are a lot of 3rd party tools to help you do this, but I’m going to show you how you can set it up just through code.

Loading the images

The first thing we need to do is preload the images we will be using. I’ve picked two bricks to use to make our platforms.

Setting Up the Grid

I’m going to use an AlignGrid class to position things by a percentage of the screen’s width. You can read more about AlignGrid here.

When we use the showNumbers() function of the grid, we get a layout grid to see where to place our game objects.

placing a block

Let’s start of with placing a single block. Let’s make a function that will do 3 things.

  • Add the image to the scene
  • Position the block
  • Scale the block to a percentage of the game’s width

You can test out the function by adding this line at the end of the create function

Making a floor

If we had to a single line of code for each block it would take up a lot up a lot of room. Fortunately, we can simply loop through a start and an end value and call the placeBlock() function from that loop.

Now we can make our bottom floor by calling the makeFloor() function from the end of the create function.

Adding Gravity

Now that we have our floor we can make the ninja fall towards it. First, let’s scale the ninja.

The Align class comes as part of the Utility Template. Basically what this function does is:

Before we can use physics in the game, we must add physics to the game’s configuration object.

This is the physics object.

Here is what it looks like as part of the entire configuration object.

Now we can set the gravity on the ninja. You can find out more about gravity in Phaser 3 here.

Add Colliders

We can set up colliders in physics to check for collisions in three different ways

  • Sprite against Sprite
  • Sprite against a Group
  • One group against another.

For this example instead of checking the ninja against every block placed we can place every block in a single block.

adding a group

We can add a group a scene in Phaser to keep a collection of game objects.

But when using physics we need to add the group to the physics object of the scene rather than the group itself.

adding the block to the group

In the placeBlock function let’s add the block to the group just after creating it.

Adding the collider

Now that we have the blocks in the group, we can set up a collider between the ninja and the brickGroup.

Making the floor stable

If you run the code now or watch the video above you’ll see that the ninja falling will push the blocks down. We can fix this by setting the blocks to immovable.

It is important that any physics that you want to apply to a sprite take place after adding that sprite to a group. If you add a sprite to a group, all physics properties are reset to default.

Platform Games Phaser 3

Complete Code

Here is the complete SceneMain code for reference. Later we will go more into building platform games with Phaser 3

Leave a Reply