Scaling Games in Phaser 3 with an Alignment Grid

Scaling Games in Phaser 3 with an Alignment Grid

As game developers, one of the challenges we have faced since the invention of the smartphone is how to scale the game so it will fit on whatever device the player has. Scaling games in Phaser 3 is no exception. There are a lot of different ways programmer will achieve this.  I’m going to show you the way I’ve been doing it for work and my own projects. I’m not saying it is absolutely the best way, but it works best for me. It is also an easy concept to explain because it doesn’t rely on anything built into Phaser itself, but rather simple mathematics. It will work on any platform where you can measure the widths and heights of objects and of the canvas itself. I’ll be using the Phaser 3 basic template to jump-start the project. This template has a static size on computers and a dynamic one on mobile. In other words, the canvas size matches the screen size on a phone or tablet.

This was originally a project I did for Phaser CE but I’ve made a lot of improvements since then.

How does the Alignment Grid work?

Using a dynamic canvas size means we don’t know the size of the game when we start. The alignment grid works because instead of placing an object at a static point we place it at a percentage of the canvas size. Say for example a sprite at an x position of 200 and a y position of 150, we place it at 25% of the screen’s width and 50% of the screen’s height.

Even though we don’t know the size, we know there will always be percentages. The Alignment grid gives a visual way to do that. We start by setting up how many rows and how many columns we want to split the game into.

Now let’s set up the class that uses this config, the Alignment Grid. Since this is a simple object and we will be passing the scene, we don’t need to extend it with anything.

The only required parameter is the scene, everything else is optional. We will verify if the scene is present and if not, we will log an error and return. If the optional parameters are missing we will set defaults. Also, we will promote some of the config object’s properties to class level variables. this.scene = config.scene for example.

 

Doing the Math!

Now here is where the magic happens. We will take the height and width passed to the AlignmentGrid class, by default the height and width of the game, and divide it by the numbers of rows and columns and put it in a cell width variable and cell height variable.

Seeing the grid!

Now we have the numbers we need to start placing by percentage. To make it easier for scaling games in Phaser 3 it is better to be able to see the grid. Let’s make a function called show() with a single parameter to alpha the lines, so we can turn the lines down in case we need to see the objects we are positioning better.

Let’s see the grid in action now.

In your create function of sceneMain place this code

Here is the result.

Scaling Games in Phaser 3

Placing Object on the grid

Now that we have a grid we are back on familiar ground as programmers because we know how to place objects at x and y coordinates. Let’s set up a function to do that.

Set up a test object to place on the grid

This part should be fairly well known to you, we will simply load an image and make a sprite.

Now place it on the grid by using this line of code:

This will place it 2 squares over and 2 squares down, counting from 0. So this will place it in the middle of the screen

scaling a game in phaser 3

Scaling to the grid

Notice though that the face doesn’t fit in the square. Since we are dividing the width of the game by 5 to make the grid we can resize the face by scaling it related to the game’s width too.

for more on scaling in Phaser 3 please see this post.

Making it a little easier

For the purpose of scaling games in Phaser 3, this is enough, but it was slowing me down constantly counting the rows and columns of the grids. I am usually using an 11 x 11 grid, and my productivity started to slide. So I came up with a numbering system.  This function will call the show function and places numbers in each cell.

I then made a new function to place by index rather than x and y. It simply converts the index to the grid’s x and y and calls the placeAt function.

So now replace the placeAt(2,2,this.face) with this code:

and replace the grid.show() with:

Here is the result:

Testing the scaling

In your browser’s tools, resize the screen for various devices. On chrome, this is done by pressing f-12 and then clicking the mobile icon.

Always refresh the page after changing devices. There are a few occasions where I’ve had to tweak placements or sizes for certain devices, but I’ve been using this method in my professional work for almost 2 years now, and I’ve found it has done 99% of the work for me.  Although that was Phaser CE, because we are using mathematics this technique will work with any framework. I hope it helps you with scaling games in Phaser 3. Happy coding!!!

Leave a Reply