Exploring RESIZE Scale Mode in Phaser 3

We explored FIT scale mode in previous article. We already did most of the base work for calculating and scaling sprites and texts and respositioning them in the scene view area so let see how much of additional code do we need to add in order to change our game from FIT to RESIZE scale mode.

First we need to change game config as following

Now we need to add a resize event in our create method as following

Create resize method as following

In resize method we retrieve width and height of the game from “gameSize”. We will reposition the controls by simply calling “positionControls”. This change is made for both scenes. See below for the complete code of both scenes (All changes in the code from FIT mode is marked)

We also need to change tweens since they are used for moving sprites. If tweens are running while game is resized, then we need to change target position of those tweens to point to new calculated positions. The alternate solution would be to keep a reference of tweens and cancel tweens on resize event. We can then simply reposition controls in the game to their new positions. We will look at that in the next article. If we are cancelling tweens then we also need to plan for events associated with the tweens such as onComplete especially if it contains important logic which should be executed after tweens are finished running.

Check out the game in your browser window here. Try resizing it in your browser window to see scaling at work.

You can play the game here


Leave A Comment

Your email address will not be published. Required fields are marked *