aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-06-25 14:56:49 +0200
committerTom Smeding <tom.smeding@gmail.com>2018-06-25 14:56:49 +0200
commit36097212c07ba98c9ecade40195c677211010e46 (patch)
tree375a7e22e6c81206cf90a6131e921c0d17dcded1
parenta1e2e25eab551f3a8f35152dd720b37d2b828b07 (diff)
Better keydown/keyup handling
-rw-r--r--game.js25
1 files 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();
});
}