From 36097212c07ba98c9ecade40195c677211010e46 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 25 Jun 2018 14:56:49 +0200 Subject: Better keydown/keyup handling --- game.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/game.js b/game.js index a2d5552..fa8ff23 100644 --- a/game.js +++ b/game.js @@ -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(); }); } -- cgit v1.2.3-70-g09d2