英文:
how to use two onkeydown events at the same time?
问题
我正在尝试构建一个乒乓球游戏,我希望两个球拍能够同时移动(一个用 's' 和 'w',另一个用上下箭头)。
function movePlayer1(event) {
if (player1.y > 7.5 && player1.y < 390) {
switch (event.keyCode) {
case 83: player1.y += player1.v;
break;
case 87: player1.y -= player1.v;
break;
}
} else if (player1.y <= 7.5) {
switch (event.keyCode) {
case 83: player1.y += player1.v;
break;
}
} else if (player1.y >= 390) {
switch (event.keyCode) {
case 87: player1.y -= player1.v;
break;
}
}
}
document.addEventListener("keydown", movePlayer1, false);
function movePlayer2(event) {
if (player2.y > 7.5 && player2.y < 390) {
switch (event.keyCode) {
case 40: player2.y += player2.v;
break;
case 38: player2.y -= player2.v;
break;
}
} else if (player2.y <= 7.5) {
switch (event.keyCode) {
case 40: player2.y += player2.v;
break;
}
} else if (player2.y >= 390) {
switch (event.keyCode) {
case 38: player2.y -= player2.v;
break;
}
}
}
document.addEventListener("keydown", movePlayer2, false);
我尝试将它们放在一个函数中,但没有帮助。
英文:
I'm trying to build a pong game, and I want the boards to be able to move simultaneously (one with the 's' and 'w' and the other with the up and down arrows).
function movePlayer1(event) {
if (player1.y > 7.5 && player1.y < 390) {
switch (event.keyCode) {
case 83: player1.y += player1.v;
break;
case 87: player1.y -= player1.v;
break;
}
}
else if (player1.y <= 7.5) {
switch (event.keyCode) {
case 83: player1.y += player1.v;
break;
}
}
else if (player1.y >= 390) {
switch (event.keyCode) {
case 87: player1.y -= player1.v;
break;
}
}
}
document.addEventListener("keydown", movePlayer1, false);
function movePlayer2() {
if (player2.y > 7.5 && player2.y < 390) {
switch (event.keyCode) {
case 40: player2.y += player2.v;
break;
case 38: player2.y -= player2.v;
break;
}
}
else if (player2.y <= 7.5) {
switch (event.keyCode) {
case 40: player2.y += player2.v;
break;
}
}
else if (player2.y >= 390) {
switch (event.keyCode) {
case 38: player2.y -= player2.v;
break;
}
}
}
document.addEventListener("keydown", movePlayer2, false);
I've tried putting them in one function instead but it didn't help.
答案1
得分: 0
有两个因素会导致您的代码行为不如您期望的那样。
自动重复仅适用于最后按下的键。比如,如果您按下"A"并按住它,自动重复将生成"A"的键按下事件。但如果您同时按下"B"并保持按住,自动重复将仅为"B"生成连续的键按下事件。另一方面,我相信在MacOS上根本不会自动重复,所以最好不要依赖于这一点。
但实际上,"keyup"事件会在多个键被按下并保持按住时被正确触发。
因此,您可以重构您的代码:不要仅依赖于keyup/keydown事件,您需要一些定时器,并且每个玩家模型将是
{
directionIsUp: true | false,
isMoving: true | false
}
所以在keydown
事件中,您设置适当的方向,并将isMoving
设置为true
。在keyup
事件中,您将isMoving
设置为false
。
定时器将根据这些模型重新渲染您的战场 - 要么移动玩家,要么将其保持在原地。
英文:
There are two things make you code behave not as you expect.
Autorepeating works only for last pressed key. Say if you press "A" and hold it autorepeating will generate keydown events for "A". But if you press "B" and hold both autorepeating will generate sequential keydown automatically for "B" only. On the other side I believe for MacOS it will not autorepeat at all so better not rely on this.
But actually "keyup" are triggered correctly even if mutliple keys were pressed and are hold.
So you can refactor your code: instead of relying on keyup/keydown only you need some timer and each player model will be
{
directionIsUp: true | false,
isMoving: true | false
}
So on keydown
you are setting appropriate direction and make isMoving
to be true
. And on keyup
you are making isMoving
to be false.
And timer will re-render your battlefield accordingly to those models - either moving player or keeping it at the same place.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论