(function() { var lastTime = 0; var vendors = ['ms', 'moz', 'webkit', 'o']; for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; } if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) { clearTimeout(id); }; }()); var Game = new function() { var boards = []; // Game Initialization this.initialize = function(canvasElementId,sprite_data,callback) { var ctx = this.ctx = AppMobi.canvas.getContext("2d"); //wrap in try/catch because XDK does not currently support this property try { ctx.HTML5CompatibilityMode = true; } catch(e){} this.ctx.width = 320; this.ctx.height = 480; this.playerOffset = 10; this.canvasMultiplier= 1; this.mobile = true; this.width = 320; this.height= 480; this.loop(); if(this.mobile) { this.setBoard(4,new TouchControls()); } SpriteSheet.load(sprite_data,callback); }; this.setKeys = function(l,r,fire) { Game.keys['left'] = l; Game.keys['right'] = r; Game.keys['fire'] = fire; }; var lastTime = new Date().getTime(); var maxTime = 1/30; // Game Loop this.loop = function() { var curTime = new Date().getTime(); requestAnimationFrame(Game.loop); var dt = (curTime - lastTime)/1000; if(dt > maxTime) { dt = maxTime; } for(var i=0,len = boards.length;io2.y+o2.h-1) || (o1.x+o1.w-1o2.x+o2.w-1)); }; // Find the first object that collides with obj // match against an optional type this.collide = function(obj,type) { return this.detect(function() { if(obj != this) { var col = (!type || this.type & type) && board.overlap(obj,this); return col ? this : false; } }); }; }; var Sprite = function() { }; Sprite.prototype.setup = function(sprite,props) { this.sprite = sprite; this.merge(props); this.frame = this.frame || 0; this.w = SpriteSheet.map[sprite].w; this.h = SpriteSheet.map[sprite].h; }; Sprite.prototype.merge = function(props) { if(props) { for (var prop in props) { this[prop] = props[prop]; } } }; Sprite.prototype.draw = function(ctx) { SpriteSheet.draw(ctx,this.sprite,this.x,this.y,this.frame); }; Sprite.prototype.hit = function(damage) { this.board.remove(this); }; var Level = function(levelData,callback) { this.levelData = []; for(var i =0; i curShip[1]) { remove.push(curShip); } else if(curShip[0] < this.t) { // Get the enemy definition blueprint var enemy = enemies[curShip[3]], override = curShip[4]; // Add a new enemy with the blueprint and override this.board.add(new Enemy(enemy,override)); // Increment the start time by the gap curShip[0] += curShip[2]; } idx++; } // Remove any objects from the levelData that have passed for(var i=0,len=remove.length;i 0) { zeros += "0"; } ctx.fillText(zeros + txt,10,20); ctx.restore(); }; this.step = function(dt) { }; };