Where am I going wrong with getting my player to move without the delay, I read some articles online "Is it possible to make JQuery keydown respond faster?" and from what I could pick up a game loop was needed so I created one but I keep getting an error in my console Uncaught ReferenceError: game is not defined I don't know why can anyone help me in finding where I'm going wrong. Also I'm using mainly jQuery so a solution using jQuery would be much appreciated.
Here is a link to the game Here is my JS:
var increments = 10;
function game(){
// Put player on map
var player = '<div id="player" class="img-responsive"></div>';
$("#map").append(player);
// Set layout for player controlls
$(document).keyup(function (e){
e.preventDefault();
$("#player.img-responsive").removeClass("run");
$("#player.img-responsive").removeClass("hit");
delete e.which;
});
$(document).keydown(function (e) {
// Use this to identify what button the user is pressing on the keyboard
// alert(e.keyCode);
// get the position of the character
var position = $("#player").position();
switch (e.which)
{
case 37: // Arrow Left
if(position.left >= increments) {
$("#player").css('left', position.left - increments + 'px');
$("#player.img-responsive").addClass("run").addClass("flip");
}
break;
case 38: // Arrow Up
if(position.top <= increments) {
$("#player").css('top', position.top - increments + 'px');
}
break;
case 39: // Arrow Right
if(position.left < 550) { // Right constraint
$("#player").css('left', position.left + increments + 'px');
$("#player.img-responsive").addClass("run").removeClass("flip");
}
break;
case 40: // Arrow Down
if(position.top < 1000) { // Bottom constraint
$("#player").css('top', position.top + increments + 'px');
}
break;
case 90: //right ctrl
$("#player.img-responsive").addClass("hit");
break;
default: return;
} // End Switch
}); // End KeyDown function
}// end game function
var game = game();
// set frame loop
setInterval(game, 1000 / 60);
Thanks in advance.