From 4d294cf9b93056a3f8910fcc061f1dad18b67398 Mon Sep 17 00:00:00 2001 From: Gabriele Cirulli Date: Sat, 22 Mar 2014 16:11:19 +0100 Subject: [PATCH] remove space to restart game, refactor code slightly --- index.html | 2 +- js/keyboard_input_manager.js | 69 +++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/index.html b/index.html index 14ec922..557b68e 100644 --- a/index.html +++ b/index.html @@ -22,7 +22,7 @@
0
-

Join the numbers and get to the 2048 tile!

+

Join the numbers and get the 2048 tile!

New Game
diff --git a/js/keyboard_input_manager.js b/js/keyboard_input_manager.js index cf67028..4f2e856 100644 --- a/js/keyboard_input_manager.js +++ b/js/keyboard_input_manager.js @@ -39,16 +39,17 @@ KeyboardInputManager.prototype.listen = function () { 39: 1, // Right 40: 2, // Down 37: 3, // Left - 75: 0, // vim keybindings - 76: 1, - 74: 2, - 72: 3, + 75: 0, // Vim up + 76: 1, // Vim right + 74: 2, // Vim down + 72: 3, // Vim left 87: 0, // W 68: 1, // D 83: 2, // S 65: 3 // A }; + // Respond to direction keys document.addEventListener("keydown", function (event) { var modifiers = event.altKey || event.ctrlKey || event.metaKey || event.shiftKey; @@ -59,38 +60,32 @@ KeyboardInputManager.prototype.listen = function () { event.preventDefault(); self.emit("move", mapped); } - - if (event.which === 32) self.restart.bind(self)(event); } }); - var retry = document.querySelector(".retry-button"); - retry.addEventListener("click", this.restart.bind(this)); - retry.addEventListener(this.eventTouchend, this.restart.bind(this)); - - var restart = document.querySelector(".restart-button"); - restart.addEventListener("click", this.restart.bind(this)); - restart.addEventListener("touchend", this.restart.bind(this)); - - var keepPlaying = document.querySelector(".keep-playing-button"); - keepPlaying.addEventListener("click", this.keepPlaying.bind(this)); - keepPlaying.addEventListener("touchend", this.keepPlaying.bind(this)); + // Respond to button presses + this.bindButtonPress(".retry-button", this.restart); + this.bindButtonPress(".restart-button", this.restart); + this.bindButtonPress(".keep-playing-button", this.keepPlaying); - // Listen to swipe events + // Respond to swipe events var touchStartClientX, touchStartClientY; var gameContainer = document.getElementsByClassName("game-container")[0]; gameContainer.addEventListener(this.eventTouchstart, function (event) { - if (( !window.navigator.msPointerEnabled && event.touches.length > 1) || event.targetTouches > 1) return; - - if(window.navigator.msPointerEnabled){ - touchStartClientX = event.pageX; - touchStartClientY = event.pageY; + if ((!window.navigator.msPointerEnabled && event.touches.length > 1) || + event.targetTouches > 1) { + return; // Ignore if touching with more than 1 finger + } + + if (window.navigator.msPointerEnabled) { + touchStartClientX = event.pageX; + touchStartClientY = event.pageY; } else { - touchStartClientX = event.touches[0].clientX; - touchStartClientY = event.touches[0].clientY; + touchStartClientX = event.touches[0].clientX; + touchStartClientY = event.touches[0].clientY; } - + event.preventDefault(); }); @@ -99,15 +94,19 @@ KeyboardInputManager.prototype.listen = function () { }); gameContainer.addEventListener(this.eventTouchend, function (event) { - if (( !window.navigator.msPointerEnabled && event.touches.length > 0) || event.targetTouches > 0) return; + if ((!window.navigator.msPointerEnabled && event.touches.length > 0) || + event.targetTouches > 0) { + return; // Ignore if still touching with one or more fingers + } var touchEndClientX, touchEndClientY; - if(window.navigator.msPointerEnabled){ - touchEndClientX = event.pageX; - touchEndClientY = event.pageY; + + if (window.navigator.msPointerEnabled) { + touchEndClientX = event.pageX; + touchEndClientY = event.pageY; } else { - touchEndClientX = event.changedTouches[0].clientX; - touchEndClientY = event.changedTouches[0].clientY; + touchEndClientX = event.changedTouches[0].clientX; + touchEndClientY = event.changedTouches[0].clientY; } var dx = touchEndClientX - touchStartClientX; @@ -132,3 +131,9 @@ KeyboardInputManager.prototype.keepPlaying = function (event) { event.preventDefault(); this.emit("keepPlaying"); }; + +KeyboardInputManager.prototype.bindButtonPress = function (selector, fn) { + var button = document.querySelector(selector); + button.addEventListener("click", fn.bind(this)); + button.addEventListener(this.eventTouchend, fn.bind(this)); +}; -- GitLab