O'Reilly logo

Building HTML5 Games with ImpactJS by Jesse Freeman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mobile Web Support

While it is possible to also run Impact on mobile browsers, there are a lot of technical challenges, especially on Android devices, that make it difficult to cover in this book. Impact does come with some easy ways to test if your game is running on a mobile device. You can test the ig.ua class for the device type the game is running in. Here is a quick example for mobile:

if( ig.ua.mobile ) {
    // Disable sound for all mobile devices
    ig.Sound.enabled = false;
}

As you can see, Impact will let us know if the game is being played on a mobile device. It is also important to make sure that you set sound support to false, because most mobile browsers, including Safari on iOS, cannot play more than one sound at a time. Also, for browsers that don’t support sound at all, you want to make sure your game doesn’t try to load them or it may crash.

You can also use the ig.ua class to test if the game is running on the iPhone 4. This is important because of the iPhone’s retina display. In this case, you will want to increase the game’s scale like so:

if( ig.ua.iPhone4 ) {
    // The iPhone 4 has more pixels - we'll scale the
    // game up by a factor of 4
    ig.main('#canvas', MyGame, 60, 160, 160, 4);
}
else if( ig.ua.mobile ) {
    // All other mobile devices
    ig.main('#canvas', MyGame, 60, 160, 160, 2);
}
else {
    // Desktop browsers
    ig.main('#canvas', MyGame, 60, 240, 160, 2);
}

There is a lot to cover on running your game in mobile browsers, so read through Impact’s documentation (http://impactjs.com/documentation/impact-on-mobile-platforms ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required