Use textarea in Phaser Game

In order to use textboxes in canvas we can use html input components and position them over the canvas. I recently had a requirement to display a message board resembling “textarea” in a Phaser game. The game was responsive so it also required repositioning and resizing of the textarea. I am posting bare minimum code to achieve this fuctionality here. Add following html

We have added a textarea which is hidden initially. We will display this component after resizing and repositioning inside the game. Now add css as following in app.css

We can customize scrollbar styles to make it look nicer using webkit scrollbar properties. You can change these styles to match your game. This works on chrome and safari. Other browsers do not support these properties but you can still stylize it using normal css styles to match your game. Now add the game code as following


Quick Tip

Shun window.onload in HTML5 games

A lot of javascript developers have been using window.onload casually in their code but this is something which we should completely avoid since window.onload is only called once and if there are multiple window.onload events, then only the last onload event gets called. In our Phaser games we normally initialize the game as following

window.onload is called after the main HTML, all CSS files, images and other resources have been loaded and rendered. We don’t want to stall loading of the game and its resources until everything else on the page has been loaded and rendered so as soon as initial HTML document has been loaded and parsed, we can start loading our game without waiting for page stylesheets, images, etc to finish loading. An alternate approach for this using vanilla javascript would be

This starts loading the game as soon as HTML DOM is parsed and ready and no fear of having to change[…]

App Promotion Trick for Windows Store App

If you are a publisher who has published multiple UWP apps in the windows store and want to add a catalog of all your other apps in your UWP app, then there is no need to write a lot of code to achieve this capability. Microsoft has provided URI scheme to launch the Microsoft Store app to specific pages in the store. We can use search capability to find products from a specific publisher and simply launch that URI using LaunchUriAsync API method. For ex. in order to launch all products published by some “XYZ Corporation” in the windows store we can simply call the following URI ms-windows-store://publisher/?name=XYZ Corporation Following code example demonstrates how this can be achieved using 3 lines of code in Javascript store app

Simply call this method on on some action such as a button click and it will launch a new popup window containing all apps from[…]

Open Share Dialog from Universal Windows Javascript App (UWP)

Opening Share Dialog Popup in Windows Universal App is very simple and with a few lines of code, this functionality can be quickly added to any app. The code below can be used to share an app’s web URL (you can use MS Store URL of the app as well) in UWP Javascript application

In the application page ready event we need to initialize DataTransferManager and attach an event listener for “datarequested”. In that method you can set title, display, web link properties which will be used by the dialog popup. Store ID of the app can be found from “App Management -> App Identity” page of the specific app in your developer account. In order to launch Share Dialog Popup simply call “showShareUI” method on some action (such as some button click etc). This will open dialog box as following Share Dialog Popup displays the title and description at the[…]

Quick Tip

Display CPMStar ads at the start of HTML5 game

Create a css file as following

Create game HTML as following

Replace cpmstar_pid with your pid The ad is initialized and loaded as soon as HTML is loaded. The ad is positioned on top of HTML5 game area. We will initialized a timer which will enable closeAd button after a predefined interval (normally 5 or 10 secs)

Below is the skeleton game code.


Quick Tip

WebP Image Fallback Options

WebP is a relatively new image format which provides lossless and lossy compression for web images. It was developed and released by Google in 2010. Accroding to Google WebP format saves around 25-30% of image size which is a big saving for image-heavy sites. Even for normal sites, it saves a lot of network bandwidth and results in overall performance improvement of a web site (in turn a better ranking by search engines). Even though this format has been there since 2010, it is still not supported by all browsers.The good thing is that it is natively supported in Google Chrome and the Opera browsers which cover for bigger chunk of browser market share. For mobile sites this format has become a necessity to optimize load time of websites on mobile. Google provides tools to convert images from one format(png, jpg etc) to webp and viceversa If we are using webp format of[…]

Compile Cordova App for iOS

I recently planned to compile my Phaser games for iOS App Store. These games were build using Cordova CLI and have already been published on Android Store so I already have a functioning game which was to be ported to iOS. I decided to document the steps I had to follow to get my game compiled and published to iOS App Store. You can develop the game on any platform of your liking but in order to compile and publish these games to iOS App Store you do need to have a Mac. It can not be done from Windows or any other platform. Steps to compile the game: Download MacOS installer for Node.js and install it on your Mac. Go to App Store on your Mac and install latest version of XCode. You need XCode to build the game. Open Terminal console (Finder -> Applications ->Utilities) on your MacOS and install cordova using npm utility of Node.js by[…]

Phaser RESIZE Scale mode not resizing the game on mobile

I had minimal html markup for a web game which used RESIZE scale mode as following

The game was started with the following statement

code of Boot class

When the game was loaded initially on mobile, it took the available space on the mobile screen for that orientation but turning the mobile device to other orientation did not correctly fit in the available space. Initially it looked like an issue with the width and height values received by the resize method of the game state as it was taking the correct width of the device but the height was proportaionate to game size in previous orientation. There was no other css applied to the div ids. When I checked width and height of “game-container”, it was also showing incorrect width and height for the available screen space. It looked like width was seen correctly but height was calculated[…]

How to avoid screen flicker when embedding responsive Phaser game in Bootstrap site

Phaser games can be responsive and will fit in the available space which makes it easy to embed in variety of devices with different sizes. When Phaser games are embeded in a Bootstrap site which is responsive, there can be some flickering on the web page when game is initially loaded. When Phaser games are loaded, it loads in the actual size it was created in and afterwards CANVAS is resized to fit in the available space. The example below is a bootstrap template in which Phaser game is embeded in half of the screen and remaining half screen has some other content. Click the link to load the page to see how the game is being resized to fit in the available space. To make the game load in a nice manner without visible resizing, we need to intially hide the div in which game is loaded. In Phaser boot method we can[…]

How to Handle Orientation Change in Phaser Mobile Web Game

If you are not using RESIZE mode for mobile game development and using SHOW_ALL or other scale modes, then you would need to lock the game for one specific orientation. In the previous articles we discussed about the mobile game which was to be published to Android store (Intoduction, Calculation, Game Screen), I also uploaded the game online. The game was played in Landscape mode and I initially developed it for desktop. When I opened it in mobile, though I could open it in Portrait mode and everything was calculated well, I wanted to lock it for Landscape orientation when opened in browser on a mobile device. The orientation was easily locked in Android app by simply changing some configuration option but for browser version I had to write some addtional code. I got some help from Emanuele’s blog on the same topic and borrowed some code. I had to handle it differently[…]