如何同时使用两个onkeydown事件?

huangapple go评论75阅读模式
英文:

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 &gt; 7.5 &amp;&amp; player1.y &lt; 390) {
                    switch (event.keyCode) {
                        case 83: player1.y += player1.v;
                            break;
                        case 87: player1.y -= player1.v;
                            break;
                    }
                }
                else if (player1.y &lt;= 7.5) {
                    switch (event.keyCode) {
                        case 83: player1.y += player1.v;
                            break;
                    }
                }
                else if (player1.y &gt;= 390) {
                    switch (event.keyCode) {
                        case 87: player1.y -= player1.v;
                            break;
                    }
                }
            }
            document.addEventListener(&quot;keydown&quot;, movePlayer1, false);

            function movePlayer2() {
                if (player2.y &gt; 7.5 &amp;&amp; player2.y &lt; 390) {
                    switch (event.keyCode) {
                        case 40: player2.y += player2.v;
                            break;
                        case 38: player2.y -= player2.v;
                            break;
                    }
                }
                else if (player2.y &lt;= 7.5) {
                    switch (event.keyCode) {
                        case 40: player2.y += player2.v;
                            break;
                    }
                }
                else if (player2.y &gt;= 390) {
                    switch (event.keyCode) {
                        case 38: player2.y -= player2.v;
                            break;
                    }
                }
            }
            document.addEventListener(&quot;keydown&quot;, 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.

huangapple
  • 本文由 发表于 2020年1月7日 02:01:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定