-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
41 lines (37 loc) · 1.35 KB
/
Copy pathcontroller.js
File metadata and controls
41 lines (37 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Controller {
constructor() {
this.upBtn = document.getElementById('up');
this.downBtn = document.getElementById('down');
this.leftBtn = document.getElementById('left');
this.rightBtn = document.getElementById('right');
}
set({ up, down, left, right, kill }) {
this.upBtn.addEventListener('click', _ => { up(); this.upBtn.blur(); });
this.downBtn.addEventListener('click', _ => { down(); this.downBtn.blur(); });
this.leftBtn.addEventListener('click', _ => { left(); this.leftBtn.blur(); });
this.rightBtn.addEventListener('click', _ => { right(); this.rightBtn.blur(); });
const handleKeydown = ev => {
switch (ev.keyCode) {
case 37: return left();
case 38: return up();
case 39: return right();
case 40: return down();
case 27: return kill();
}
};
document.addEventListener('keydown', handleKeydown);
this.up = up;
this.down = down;
this.left = left;
this.right = right;
this.kill = kill;
this.handleKeydown = handleKeydown;
}
removeHandlers() {
document.removeEventListener('keydown', this.handleKeydown);
this.upBtn.removeEventListener('click', this.up);
this.downBtn.removeEventListener('click', this.down);
this.leftBtn.removeEventListener('click', this.left);
this.rightBtn.removeEventListener('click', this.right);
}
}