Skip to content

Commit 4d294cf

Browse files
remove space to restart game, refactor code slightly
1 parent 08c50df commit 4d294cf

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h1 class="title">2048</h1>
2222
<div class="best-container">0</div>
2323
</div>
2424
</div>
25-
<p class="game-intro">Join the numbers and get to the <strong>2048 tile!</strong></p>
25+
<p class="game-intro">Join the numbers and get the <strong>2048 tile!</strong></p>
2626
<a class="restart-button">New Game</a>
2727
<div class="game-container">
2828
<div class="game-message">

js/keyboard_input_manager.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ KeyboardInputManager.prototype.listen = function () {
3939
39: 1, // Right
4040
40: 2, // Down
4141
37: 3, // Left
42-
75: 0, // vim keybindings
43-
76: 1,
44-
74: 2,
45-
72: 3,
42+
75: 0, // Vim up
43+
76: 1, // Vim right
44+
74: 2, // Vim down
45+
72: 3, // Vim left
4646
87: 0, // W
4747
68: 1, // D
4848
83: 2, // S
4949
65: 3 // A
5050
};
5151

52+
// Respond to direction keys
5253
document.addEventListener("keydown", function (event) {
5354
var modifiers = event.altKey || event.ctrlKey || event.metaKey ||
5455
event.shiftKey;
@@ -59,38 +60,32 @@ KeyboardInputManager.prototype.listen = function () {
5960
event.preventDefault();
6061
self.emit("move", mapped);
6162
}
62-
63-
if (event.which === 32) self.restart.bind(self)(event);
6463
}
6564
});
6665

67-
var retry = document.querySelector(".retry-button");
68-
retry.addEventListener("click", this.restart.bind(this));
69-
retry.addEventListener(this.eventTouchend, this.restart.bind(this));
70-
71-
var restart = document.querySelector(".restart-button");
72-
restart.addEventListener("click", this.restart.bind(this));
73-
restart.addEventListener("touchend", this.restart.bind(this));
74-
75-
var keepPlaying = document.querySelector(".keep-playing-button");
76-
keepPlaying.addEventListener("click", this.keepPlaying.bind(this));
77-
keepPlaying.addEventListener("touchend", this.keepPlaying.bind(this));
66+
// Respond to button presses
67+
this.bindButtonPress(".retry-button", this.restart);
68+
this.bindButtonPress(".restart-button", this.restart);
69+
this.bindButtonPress(".keep-playing-button", this.keepPlaying);
7870

79-
// Listen to swipe events
71+
// Respond to swipe events
8072
var touchStartClientX, touchStartClientY;
8173
var gameContainer = document.getElementsByClassName("game-container")[0];
8274

8375
gameContainer.addEventListener(this.eventTouchstart, function (event) {
84-
if (( !window.navigator.msPointerEnabled && event.touches.length > 1) || event.targetTouches > 1) return;
85-
86-
if(window.navigator.msPointerEnabled){
87-
touchStartClientX = event.pageX;
88-
touchStartClientY = event.pageY;
76+
if ((!window.navigator.msPointerEnabled && event.touches.length > 1) ||
77+
event.targetTouches > 1) {
78+
return; // Ignore if touching with more than 1 finger
79+
}
80+
81+
if (window.navigator.msPointerEnabled) {
82+
touchStartClientX = event.pageX;
83+
touchStartClientY = event.pageY;
8984
} else {
90-
touchStartClientX = event.touches[0].clientX;
91-
touchStartClientY = event.touches[0].clientY;
85+
touchStartClientX = event.touches[0].clientX;
86+
touchStartClientY = event.touches[0].clientY;
9287
}
93-
88+
9489
event.preventDefault();
9590
});
9691

@@ -99,15 +94,19 @@ KeyboardInputManager.prototype.listen = function () {
9994
});
10095

10196
gameContainer.addEventListener(this.eventTouchend, function (event) {
102-
if (( !window.navigator.msPointerEnabled && event.touches.length > 0) || event.targetTouches > 0) return;
97+
if ((!window.navigator.msPointerEnabled && event.touches.length > 0) ||
98+
event.targetTouches > 0) {
99+
return; // Ignore if still touching with one or more fingers
100+
}
103101

104102
var touchEndClientX, touchEndClientY;
105-
if(window.navigator.msPointerEnabled){
106-
touchEndClientX = event.pageX;
107-
touchEndClientY = event.pageY;
103+
104+
if (window.navigator.msPointerEnabled) {
105+
touchEndClientX = event.pageX;
106+
touchEndClientY = event.pageY;
108107
} else {
109-
touchEndClientX = event.changedTouches[0].clientX;
110-
touchEndClientY = event.changedTouches[0].clientY;
108+
touchEndClientX = event.changedTouches[0].clientX;
109+
touchEndClientY = event.changedTouches[0].clientY;
111110
}
112111

113112
var dx = touchEndClientX - touchStartClientX;
@@ -132,3 +131,9 @@ KeyboardInputManager.prototype.keepPlaying = function (event) {
132131
event.preventDefault();
133132
this.emit("keepPlaying");
134133
};
134+
135+
KeyboardInputManager.prototype.bindButtonPress = function (selector, fn) {
136+
var button = document.querySelector(selector);
137+
button.addEventListener("click", fn.bind(this));
138+
button.addEventListener(this.eventTouchend, fn.bind(this));
139+
};

0 commit comments

Comments
 (0)