HTML and CSS Reference
In-Depth Information
The game needs to handle different screen resolutions. To this end, instead of drawing fixed size input
squares (which could end up being too large or too small depending on the device), the game adjusts the size
of the squares based on the width of the game. Based on some informal testing, dividing the width of the game
into five regions works well enough. Buttons are large enough to be hit easily but don't take up too much screen
real estate.
The first step is to add the controls onto the page. Open up engine.js and add the following object (as
shown in Listing 3-1 ) to the bottom.
Listing 3-1: TouchControls for Alien Invasion
var TouchControls = function() {
var gutterWidth = 10;
var unitWidth = Game.width/5;
var blockWidth = unitWidth-gutterWidth;
this.drawSquare = function(ctx,x,y,txt,on) {
ctx.globalAlpha = on ? 0.9 : 0.6;
ctx.fillStyle = "#CCC";
ctx.fillStyle = "#FFF";
ctx.textAlign = "center";
ctx.globalAlpha = 1.0;
ctx.font = "bold " + (3*unitWidth/4) + "px arial";
this.draw = function(ctx) {;
var yLoc = Game.height - unitWidth;
"\u25C0", Game.keys['left']);
this.drawSquare(ctx,unitWidth + gutterWidth,yLoc,
this.step = function(dt) { };
Search WWH ::

Custom Search