Skip to content

Commit

Permalink
Added requestAnimFrame, dbl click fix and reload time fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cykod committed Feb 7, 2012
1 parent 995fba0 commit d5adf40
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
42 changes: 38 additions & 4 deletions engine.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
(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 = [];

Expand Down Expand Up @@ -47,18 +73,22 @@ var Game = new function() {
},false);
};


var lastTime = new Date().getTime();
var maxTime = 1/30;
// Game Loop
this.loop = function() {
var dt = 30 / 1000;
setTimeout(Game.loop,30);
this.loop = function(curTime) {
requestAnimationFrame(Game.loop);
var dt = (curTime - lastTime)/1000;
if(dt > maxTime) { dt = maxTime; }

for(var i=0,len = boards.length;i<len;i++) {
if(boards[i]) {
boards[i].step(dt);
boards[i].draw(Game.ctx);
}
}

lastTime = curTime;
};

// Change an active game board
Expand Down Expand Up @@ -390,6 +420,10 @@ var TouchControls = function() {
Game.canvas.addEventListener('touchmove',this.trackTouch,true);
Game.canvas.addEventListener('touchend',this.trackTouch,true);

// For Android
Game.canvas.addEventListener('dblclick',function(e) { e.preventDefault(); },true);
Game.canvas.addEventListener('click',function(e) { e.preventDefault(); },true);

Game.playerOffset = unitWidth + 20;
};

Expand Down
6 changes: 3 additions & 3 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var enemies = {
circle: { x: 250, y: -50, sprite: 'enemy_circle', health: 10,
A: 0, B: -100, C: 1, E: 20, F: 100, G: 1, H: Math.PI/2 },
wiggle: { x: 100, y: -50, sprite: 'enemy_bee', health: 20,
B: 50, C: 4, E: 100 },
B: 50, C: 4, E: 100, firePercentage: 0.001, missiles: 2 },
step: { x: 0, y: -50, sprite: 'enemy_circle', health: 10,
B: 150, C: 1.2, E: 75 }
};
Expand Down Expand Up @@ -209,7 +209,7 @@ Enemy.prototype.type = OBJECT_ENEMY;

Enemy.prototype.baseParameters = { A: 0, B: 0, C: 0, D: 0,
E: 0, F: 0, G: 0, H: 0,
t: 0, reloadTime: 20,
t: 0, reloadTime: 0.75,
reload: 0 };

Enemy.prototype.step = function(dt) {
Expand Down Expand Up @@ -237,7 +237,7 @@ Enemy.prototype.step = function(dt) {
}

}
this.reload--;
this.reload-=dt;

if(this.y > Game.height ||
this.x < -this.w ||
Expand Down

0 comments on commit d5adf40

Please sign in to comment.