reduce method calls
var left1 = a.attr('x'),
left2 = b.attr('x'),
right1 = a.attr('x') + a.attr('width'),
right2 = b.attr('x') + b.attr('width'),
top1 = a.attr('y'),
top2 = b.attr('y'),
bottom1 = a.attr('y') + a.attr('height'),
bottom2 = b.attr('y') + b.attr('height');
Could be optimized like so:
var left1 = a.attr('x'),
left2 = b.attr('x'),
right1 = left1 + a.attr('width'),
right2 = left2 + b.attr('width'),
top1 = a.attr('y'),
top2 = b.attr('y'),
bottom1 = top1 + a.attr('height'),
bottom2 = top2 + b.attr('height');
This saves you 4 method calls per hitDetection
call. Same applies to wallDetection
and probably other functions as well. In fact, I also believe you width and height calls can be removed and just cached through a closure, since those are pretty static after creation, see next example.
Also with the next bit:
var animateEnemies = function(enemy) {
var enemyWidth = enemy.attr('width'),
enemyHeight = enemy.attr('height'),
...
You set the width and height of the enemies once, so they seem pretty constant, you could remove the .attr()
lookup and pass the width and height from the createEnemies
call as well.
var animateEnemies = function(enemy , enemyWidth , enemyHeight) {
var animationDuration = getRandomInt(1000, 3000) / difficulty;
enemy.animate({
x: getRandomInt(0, gameWidth - enemyWidth),
y: getRandomInt(0, gameHeight - enemyHeight)
}, animationDuration, "ease-in-out");
// use setTimout instead of onAnimation callback, since it gave "too much recursion" in Firefox 3.6
this.timeOut = setTimeout(function() {
animateEnemies(enemy , enemyWidth , enemyHeight);
}, animationDuration);
};
Reducing function calls and caching variables can help a lot in legacy browsers, other then that, code looks pretty neat.