I'm having a problem with getting an input from cursor keys on a keydown event. I am using a keydown event for spacebar and the arrow keydown event at the same time (in a spaceinvaders game, you should be able to move AND fire via spacebar at the same time).
The ship should be able to move along the x and y axis at the same time while spacebar is pressed. Now the actual problem: whenever spacebar is pressed to fire, I can move up and forward, but not forward and down. I also can move backwards and down but not backwards and up. when the spacebar is not pressed it works and I can move diagonally. The same applies for wasd keys.
if (e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40)
e.preventDefault();
if (e.keyCode == 32){
e.preventDefault();
spaceState = true;
}
if (e.keyCode == 87 || e.keyCode == 38) keyState["UpDown"]="w";//w
if (e.keyCode == 83 || e.keyCode == 40) keyState["UpDown"]="s";//s
if (e.keyCode == 65 || e.keyCode == 37) keyState["RightLeft"]="a";//a
if (e.keyCode == 68 || e.keyCode == 39) keyState["RightLeft"]="d"; //d
combination w and d works combination s and a works
w,a and s,d DONT work
Still all combinations work when spacebar is not pressed, wasd combinations work even when the spacebar is returning a keydown event. The question simply is why?