Making of a responsive game in Phaser: Part 3

In part one and part two of this article series, we designed “Main Menu” screen and did some code to manage scaling of it for various resolutions and aspect ratios. “Main Menu” screen followed the same layout for both portrait and landscape orientations.

In part three we will be working on actual “Game” screen and implement different layouts for portrait and landscape orientations. The game is a number puzzle which contains a 6×6 number tiles grid, 2 additional tiles for displaying current and best scores and another 3 icons for various options.

In the image below you will see the alignment of various elements in portrait and landscape orientations. The layouts are different here. In portrait orientation the grid is centered with other elements placed on top and bottom of it while in landscape orientation the grid is moved to the left which takes 2/3rd of the available width and rest of the elements are stacked on top of each other in the remaining 1/3rd space.

Let us look at the code below for key points.

Screenshots of portrait and landscape orientations are given below. You can check demo of “Game” screen on your device here.

We looked at “getSpriteScale” in part two of this article series. There is one change to this method. We are passing an additional parameter “isFullScreen” which will stretch sprite to fit into the available space if available width and height is more than the sprite size. This is required since the number tiles are required to be exact fit in the available space. Current number tiles are assumed to be square but this method can be changed for rectangular tiles accordingly. We don’t have any such need at the moment so we will leave it as-is.

In “positionControls” method we are aligning controls according to their orientation. It actually does not matter whether the game is running on pc or mobile device. All we care for is the available width and height for the game and shall resize everything else accordingly.

There is a landscape ratio we are using in the game which is used to decide when to switch to portrait mode.

In portrait mode

  • Puzzle grid is centered and uses the height same as it width.
  • Remaining space is divided into upper section and lower section.
  • Upper section contains current and high score tiles.
  • Lower section contains 3 icons for various options.

In landscape mode

  • Puzzle grid is left aligned and is allocated 2/3rd of the available space.
  • Remaining 1/3rd space contains current and high score tiles as well as 3 icons for various options which are stacked on top of each other and center aligned to the 1/3rd space.

Next

Working on finishing the game and will post the link to finished game for you to play. Until then.. Happy coding..

Update: I finished the game and a few things changed during the process. Info icon was removed and some more minor changes to the alignment of individual elements but the basic concept remains the same. Check out finished game link below

Number Spread


10 thoughts on “Making of a responsive game in Phaser: Part 3

  1. Awesome tutorial! Thanks for tackling this problem that everyone seems to have yet nearly no comprehensive resource on how to solve it.

    I’ve noticed that with devices with low base resolutions and a high pixel ratio, the graphics become blurry. Any way to fix this?

    1. If you are using viewport meta tag in HTML and setting width to device-width, you will only get CSS width and height in the method which scales all assets down. If it is possible to not use viewport meta tag to device-width, it will take the actual pixel size (and not CSS width and height) on mobile, which is going to work just fine. In case viewport is set to device-width, I suggest having another set of assets optimized for low resolution.

        1. I see you have chosen base size as 1080×1920 but your images are quite small largest being around 10% of this size. There are two things you need to consider. If you are going for higher resolution (something like 1080×1920) then your assets should be big enough to fit in entire real estate and then only there is an advantage of not using device-width. If you have assets which can only fit in a space of something like 320×480, I would advise using the device-width. RESIZE method gives you width and height accordingly so let say your device size is 320×480 but pixel size is 1080×1920, if you don’t use device-width, you see size as 1080×1920 in your game but if you use device width, then you see size as 320×480 in your game, its just the scaling of images which is making the game look blurry. Increase the size of your assets and you would see crispy clear game on high pixel ratio devices. In fact pixel ratio won’t matter at all if you don’t use device-width since you get pixel size only in your game so game is perfectly scaled.

          If you could share a working web URL, I could check it on my iPhone. Can you try my game on your device to see if there is any distortion? I might be interested in scaling my game down to something like 320×480 and then use device-width to see how it looks on high pixel ratio devices.

  2. Your example looks crisp, and I increased the asset sizes which didn’t work. The problem for me is that it is still using the device size, not the pixel size, so it is scaling down too much to match the small resolution. For example, using the chrome device toolbar on a galaxy s5 returns height of 640 for mine but 2560 for yours. I’m not sure how to fix this since I’m not using device width in the viewport tag.

    I’m confused about what setting the base resolution does, because doesn’t RESIZE just ignore that and set the resolution based on the parent container?

    1. You are correct. Base resolution is ignored by RESIZE. Base resolution is only to make sure all assets are prepared using that resolution as a baseline. Your assets will ultimately be resized to the width and height you see in RESIZE method. Web games may not have the issue which you are facing since they are using viewport configuration correctly. It might be issue with the Android webview. You must have some configuration option to ignore device-width and let your app see the pixel size. I don’t have much experience with Android app and cordova so can’t help there. Did you try removing viewport tag completely? You should see same height in your game which you see in mine.

      1. Hmm, well I just manually set the width and height based on the device pixel size with SHOW_ALL since I only plan on using mobile and nothing will resize after the first load.

        Thanks for the great tutorial and responding to my questions!

        1. SHOW_ALL works great for most cases except the letter boxing. If you can cover background behind the game with something matching with your game and you are using only one orientation, then SHOW_ALL works great. In fact better than RESIZE. RESIZE is for different layouts for different orientations and using the full screen space but needs much more calculations and scenarios to handle which is not required in SHOW_ALL at all.

Leave A Comment

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