I got my code to work, and you can control an element using the arrow keys. However, i only want my element to move up-down-left-right. So when i press UP, i should not be able to press RIGHT at the same time (strafing). But I'm not sure how to prevent this? Anyone that can put me in the right direction?
How i check my movement,
// Tracking keys
var keysDown = {};
// Moving the player around on the map
if(39 in keysDown) { player.x+=gameSettings.speed; } // Move Right
if(37 in keysDown) { player.x-=gameSettings.speed; } // Move Left
if(38 in keysDown) { player.y-=gameSettings.speed; } // Move Up
if(40 in keysDown) { player.y+=gameSettings.speed; } // Move Down
addEventListener('keydown', function(e) {
keysDown[e.keyCode] = true;
}, false);
addEventListener('keyup', function(e) {
delete keysDown[e.keyCode];
}, false);
I'm sure there is an easy solution, but I've been looking at this too long, so any pinpoints in the right direction would be great!