mardi 1 septembre 2015

Multiple JavaScript if statements at the same time not working as expected

I have a problem over here. I want to move around a box with arrow keys, here is the code (simplified):

if( Key Pressed = Left Arrow ) {
     move box to left
}
 if( Key Pressed = Up Arrow ) {
     move box up
}
 if( Key Pressed = Right Arrow ) {
     move box to right
}

Here is the code, not simplified:

window.addEventListener("keydown", steerAround, false);
function steerAround(a) {
if (a.keyCode == "37")  {
     move left
}
if (a.keyCode == "38") {
     move right 
} 
...

This works almost fine, the box is moving! However, it can only move in one direction at a time! Not both to the left and up for example. How can I fix this?

Aucun commentaire:

Enregistrer un commentaire