{"id":38,"date":"2017-03-29T05:49:08","date_gmt":"2017-03-29T05:49:08","guid":{"rendered":"http:\/\/www.netexl.com\/blog\/?p=38"},"modified":"2017-11-06T19:33:45","modified_gmt":"2017-11-06T19:33:45","slug":"making-of-a-responsive-game-in-phaser-part-1","status":"publish","type":"post","link":"https:\/\/www.netexl.com\/blog\/making-of-a-responsive-game-in-phaser-part-1\/","title":{"rendered":"Making of a responsive game in Phaser: Part 1"},"content":{"rendered":"<p>I first started reading about <a href=\"https:\/\/phaser.io\/\" target=\"_blank\">Phaser<\/a>\u00a0around 5 months back and since then I have been planning to write games in Phaser. After studying some material online I finally decided to write a few games to get used to the framework. I wrote a few\u00a0games which were more like practice games and then started digging further\u00a0into it to make something more polished. Finally\u00a0I decided to write a game and publish a series of articles\u00a0as I encountered scenarios which I wanted to manage in the game but\u00a0could not find enough tutorials for help.<\/p>\n<h3><strong>Scaling the game<\/strong><\/h3>\n<p>A very basic\u00a0requirement for HTML5 games is to be able to scale to the resolution of the device on which the game is running. Since HTML5 games can run both on pc as well as mobile, there are thousands of resolutions and aspect ratios which the game needs to support. Now this is somewhat hardest part of the development process. I decided to make a game which is\u00a0responsive, adapts to the screen resolution on both pc as well as mobile, and works for\u00a0both orientations (Portrait as well as Landscape) in mobile. It did not turn out to be a trivial solution.\u00a0There is some material available on internet which mostly talks about the concepts but there is not much\u00a0available out-of-the-box to use and there is no one solution which can be used in handle it. There are solutions which fit into one type of games but would not work for another. It entirely depends on the requirements of a specific game.<\/p>\n<p>Phaser has built-in support for scaling a\u00a0game to fit size of the device. The\u00a0ScaleManager in Phaser has a few options to scale a\u00a0game out-of-the-box. One such option is to\u00a0use\u00a0<em>SHOW_ALL<\/em> property which resizes the game to fit into available space using the same aspect ratio which was used to develop the game. If the device has a different aspect ratio compared to the game, then after resizing there is going to be some blank space\u00a0either around horizontal sides or the vertical sides depending upon which of the two\u00a0aspect ratios is higher (game vs device). This is known as &#8220;letter boxing&#8221; effect. This option is easiest to use and does not need developer to do anything special. Just set the property and let Phaser handle everything else. Everything gets scaled up or down in the same ratio so the game looks fine for all resolutions. The blank bars indeed don&#8217;t look nice and need some more work to blend the game into the background. Easiest option would be to set a background\u00a0in HTML which would simply blend with the game background and everything would look perfect. Another\u00a0scaling mode using <em>EXACT_FIT<\/em> does not maintain the aspect ratio which means the game will be distorted.<\/p>\n<p>The third option is to use <em>RESIZE<\/em> mode which creates canvas element for the game that is the same size as the parent container. This needs handling of the various\u00a0elements in the game itself and that is the mode which can\u00a0be used for making games truly responsive.\u00a0We are going to explore\u00a0<em>RESIZE<\/em> scale mode which has an extra feature of &#8220;Resize callback&#8221; that other scaling modes don&#8217;t. If our state has a &#8220;resize&#8221; method, it will be called every time size of the parent changes (change of orientation on mobile or, change of browser window size on pc).<\/p>\n<div style=\"height:250px;\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:inline-block;width:300px;height:250px\"\r\n     data-ad-client=\"ca-pub-0810934084049672\"\r\n     data-ad-slot=\"8927903874\"><\/ins>\r\n<script>\r\n(adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div>\n<p>As far as handling of orientation in mobile devices is concerned, one of the most common solutions used by development community is to lock one of the orientations\u00a0in mobile devices and develop the game for only one orientation so when you open the game in other orientation, it hides the game and displays an alternate screen which could be as simple as an image asking you to turn the device. Though it makes perfect sense for certain games, sometimes it is desirable to be able to play the game in both orientations.<\/p>\n<p>We are going to design the game which works for both orientations and scales to the available space. This blog\u00a0will talk mostly about the display aspect of the game.<\/p>\n<blockquote><p>If you are new to Phaser, I would recommend\u00a0reading some basic tutorials and get familiarity with the framework.<\/p><\/blockquote>\n<p>The first thing we need to decide is\u00a0the\u00a0placement of various elements. We are going to use Phaser &#8220;states&#8221; and will use a few states in the game and the first being the &#8220;Main Menu&#8221;\u00a0screen. We want to display a &#8220;Title&#8221; and &#8220;Play&#8221; button and a couple more buttons\u00a0for &#8220;Help&#8221; and &#8220;Audio On\/Off&#8221;. The layout of &#8220;Main Menu&#8221;\u00a0screen would be as following.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-57\" src=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/03\/ResponsiveGame1-259x300.png\" alt=\"Phaser Responsive Game Home\" width=\"259\" height=\"300\" srcset=\"https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/03\/ResponsiveGame1-259x300.png 259w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/03\/ResponsiveGame1-173x200.png 173w, https:\/\/www.netexl.com\/blog\/wp-content\/uploads\/2017\/03\/ResponsiveGame1.png 272w\" sizes=\"auto, (max-width: 259px) 100vw, 259px\" \/><\/p>\n<p>For simplicity we are going to divide whole vertical area into three equal parts. The top part will contain game title. Middle part will contain &#8220;Play&#8221; button. Bottom part will contain two buttons for &#8220;Help&#8221; and &#8220;Audio On\/Off&#8221;.<\/p>\n<p>Next step is to plan for the art work and pick\u00a0a base resolution. All assets in the game should be prepared in the same proportion as the base resolution so when we scale all elements in the game, everything looks normal. There can not be any\u00a0recommendation for a base resolution to\u00a0use in the game. It totally depends on specific need of a game, target audience, performance, bandwidth etc. We\u00a0are going to use\u00a0base resolution of 1920&#215;1080 for landscape (1080&#215;1920 for portrait in mobile) and prepare assets for use in the game.<\/p>\n<h3>Next<\/h3>\n<p>We will look at some code and a demo link of our &#8220;Main Menu&#8221; screen in the next article.<\/p>\n<p><a href=\"https:\/\/www.netexl.com\/blog\/making-of-a-responsive-game-in-phaser-part-2\/\">Making of a responsive game in Phaser: Part 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I first started reading about Phaser\u00a0around 5 months back and since then I have been planning to write games in Phaser. After studying some material online I finally decided to write a few games to get used to the framework. I wrote a few\u00a0games which were more like practice games and then started digging further\u00a0into it to make something more polished. Finally\u00a0I decided to write a game and publish a series of articles\u00a0as I encountered scenarios which I wanted to manage in the game but\u00a0could not find enough tutorials for help. Scaling the game A very basic\u00a0requirement for HTML5 games is to be able to scale to the resolution of the device on which the game is running. Since HTML5 games can run both on pc as well as mobile, there are thousands of resolutions and aspect ratios which the game needs to support. Now this is somewhat hardest part[&#8230;]<\/p>\n","protected":false},"author":5,"featured_media":85,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,3,4],"tags":[],"class_list":["post-38","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-framework","category-html5","category-phaser"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":4,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":603,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions\/603"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media\/85"}],"wp:attachment":[{"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netexl.com\/blog\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}