Phaser or Ionic? Por que no las dos!
Ionic allows you to build typical business style mobile applications with all the user interface elements you would expect from a native mobile application.
Why on Earth would you want to combine Phaser and Ionic?
Phaser is great at making games, but not so great for creating complex UI interfaces on mobile. Ionic is great at creating mobile applications but it’s, obviously, no good at creating games.
I’m creating a new game as a bit of an experiment which will be somewhat similar to Cookie Clicker in that it will provide what is essentially a very complex in game shop. I could create this in Phaser but it would be quite complicated, and I know I could achieve it quite easily with Ionic. So I had the wacky idea to combine them both. The end result looks a little something like this:
I think in general combining frameworks like this is a bad idea and this is a very specific use case. I think this is a good educational exercise in any case though as it will help you understand exactly how both frameworks behave within the project and where conflicts might occur.
For example, one might assume that an Ionic app (which is in effect an Angular app) is applied to the entire Document Object Model (DOM) but in fact it can be applied to just a single DIV tag if you want, whilst the rest of the DOM will behave normally.
Similarly, a Phaser game can be attached to a single DIV tag.
Creating a mini Ionic application
As I mentioned above, when you create an Ionic application the app you create only lives on the HTML node that you attach it to. In most Ionic apps the app will be attached to the BODY node in index.html:
You will then also have to create an Angular module called MyApp in app.js that is to be run through the BODY tag:
When combining Ionic and Phaser together we don’t want the Ionic app to live on the BODY tag though (at least not in this case). So that the Ionic app and Phaser app can coexist peacefully, I created a new DIV for my Ionic app to live within:
<div id="ionicControls" ng-app="MyApp"> <ion-nav-view></ion-nav-view> </div>
As is typical in an Ionic application, I’m using ion-nav-view which is essentially a placeholder to switch out the views I wish to display. In this case, all I will be displaying in here is a simple ion-footer-bar that will contain three buttons to launch three different modals.
These three modals which will serve as the various shop and settings screens will launch in full screen and overlay my Phaser application. Ionic can then be used to handle all the data storage and number crunching and Phaser can just deal with the game stuff.
Adding Phaser to the Application
When creating a Phaser game we can also specify the id of the DIV that it is to live in within the initialisation call:
game = new Phaser.Game( window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio - 45 * window.devicePixelRatio, Phaser.AUTO, 'gameArea' );
Which we can add just above where the Ionic application lives:
The complete index.html file we will come to will look a little something like this:
which has all of the necessary Ionic and Phaser files being included and loaded into the app. Now the application contains both the Ionic and Phaser frameworks simultaneously and I can use them as I see fit!