-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouchControls.js
54 lines (47 loc) · 1.29 KB
/
touchControls.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
document.addEventListener('touchstart', handleTouchStart, false);
document.addEventListener('touchmove', handleTouchMove, false);
var xDown = null;
var yDown = null;
function getTouches(evt) {
return evt.touches || // browser API
evt.originalEvent.touches; // jQuery
}
function handleTouchStart(evt) {
const firstTouch = getTouches(evt)[0];
xDown = firstTouch.clientX;
yDown = firstTouch.clientY;
};
function handleTouchMove(evt) {
if ( ! xDown || ! yDown ) {
return;
}
var xUp = evt.touches[0].clientX;
var yUp = evt.touches[0].clientY;
var xDiff = xDown - xUp;
var yDiff = yDown - yUp;
if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
if ( xDiff > 0 ) {
if (game.snake.oldDir != 2) {
game.snake.dir = 1
}
} else {
if (game.snake.oldDir != 1) {
game.snake.dir = 2
}
}
} else {
if ( yDiff > 0 ) {
if (game.snake.oldDir != 3) {
game.snake.dir = 0
}
} else {
if (game.snake.oldDir != 0) {
game.snake.dir = 3
}
}
}
game.draw()
/* reset values */
xDown = null;
yDown = null;
};