I am trying to do a simple pong game for 2 players.
The first one controls his pad with the A and Q (azerty keyboard). The second one controls his pad with the P and M.
Here is the code
function handleKeyDown(e) {
switch (e.keyCode) {
case KEYCODE_ESC:
break;
case KEYCODE_UP:
p1Bitmap.y = p1Bitmap.y - 10;
break;
case KEYCODE_DOWN:
p1Bitmap.y = p1Bitmap.y + 10;
break;
case KEYCODE_A:
p1Bitmap.y = p1Bitmap.y - 10;
break;
case KEYCODE_Q:
p1Bitmap.y = p1Bitmap.y + 10;
break;
case KEYCODE_P:
p2Bitmap.y = p2Bitmap.y - 10;
break;
case KEYCODE_M:
p2Bitmap.y = p2Bitmap.y + 10;
break;
}
}
The problem is that when player 1 presses on A and player 2 on M, the first player's key gets ignored.
It is possible to get multiple key event repeated ? (for the example of player 1 A and player 2 M I would like to get this : AMAMAMAMAMAMAMAMAMAMAMAMetc instead of just AMMMMMMMMMMMMMMM)
By the way, I am doing this for Windows 8 app (html/css/js) and CreateJS.
You could keep the movement going until you detect the corresponding keyup
event. Start moving on keydown
, don't stop until keyup
.
Also, it should be noted that keydown
is different than keypress
. keydown
will only fire once, no matter how long you hold the key down. keypress
on the other hand fires for every time a character is inserted (or would be inserted), so would see repeated events as long as you're holding the key down.