diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2018-06-25 14:56:49 +0200 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2018-06-25 14:56:49 +0200 |
commit | 36097212c07ba98c9ecade40195c677211010e46 (patch) | |
tree | 375a7e22e6c81206cf90a6131e921c0d17dcded1 | |
parent | a1e2e25eab551f3a8f35152dd720b37d2b828b07 (diff) |
Better keydown/keyup handling
-rw-r--r-- | game.js | 25 |
1 files changed, 13 insertions, 12 deletions
@@ -244,22 +244,23 @@ function advancePhysics(deltaT) { } function setupBindings() { + var up = false, down = false; + + function padUpdate() { + padVel = (down - up) * padBaseSpeed; + socket.emit("padvec", padPos, padVel); + } + window.addEventListener("keydown", function(ev) { - if (ev.key == "w" || ev.key == "ArrowUp") { - padVel = -padBaseSpeed; - socket.emit("padvec", padPos, padVel); - } else if (ev.key == "s" || ev.key == "ArrowDown") { - padVel = padBaseSpeed; - socket.emit("padvec", padPos, padVel); - } + if (ev.key == "w" || ev.key == "ArrowUp") up = true; + else if (ev.key == "s" || ev.key == "ArrowDown") down = true; + padUpdate(); }); window.addEventListener("keyup", function(ev) { - if (ev.key == "w" || ev.key == "ArrowUp" || - ev.key == "s" || ev.key == "ArrowDown") { - padVel = 0; - socket.emit("padvec", padPos, padVel); - } + if (ev.key == "w" || ev.key == "ArrowUp") up = false; + else if (ev.key == "s" || ev.key == "ArrowDown") down = false; + padUpdate(); }); } |