1
0
mirror of https://github.com/danog/2048.git synced 2024-11-27 03:44:43 +01:00

Merge pull request #16 from chrisprice/master

Replace hammer with touch event handling
This commit is contained in:
Gabriele Cirulli 2014-03-11 22:15:19 +01:00
commit db82cf4952
2 changed files with 23 additions and 21 deletions

9
js/hammer.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -57,20 +57,31 @@ KeyboardInputManager.prototype.listen = function () {
retry.addEventListener("click", this.restart.bind(this));
// Listen to swipe events
var gestures = [Hammer.DIRECTION_UP, Hammer.DIRECTION_RIGHT,
Hammer.DIRECTION_DOWN, Hammer.DIRECTION_LEFT];
var touchStartClientX, touchStartClientY;
var gameContainer = document.getElementsByClassName("game-container")[0];
var handler = Hammer(gameContainer, {
drag_block_horizontal: true,
drag_block_vertical: true
gameContainer.addEventListener("touchstart", function(event) {
if (event.touches.length > 1) {
return;
}
touchStartClientX = event.touches[0].clientX;
touchStartClientY = event.touches[0].clientY
event.preventDefault();
});
handler.on("swipe", function (event) {
event.gesture.preventDefault();
mapped = gestures.indexOf(event.gesture.direction);
if (mapped !== -1) self.emit("move", mapped);
gameContainer.addEventListener("touchmove", function(event) {
event.preventDefault();
});
gameContainer.addEventListener("touchend", function(event) {
if (event.touches.length > 0) {
return;
}
var dx = event.changedTouches[0].clientX - touchStartClientX;
var absDx = Math.abs(dx);
var dy = event.changedTouches[0].clientY - touchStartClientY;
var absDy = Math.abs(dy);
if (Math.max(absDx, absDy) > 10) {
self.emit("move", absDx > absDy ? (dx > 0 ? 1 : 3) :
(dy > 0 ? 2 : 0)); // (right : left) : (down : up)
}
});
};