From 032738f8ad1e592e373a1d85be3813b0f7c33c09 Mon Sep 17 00:00:00 2001 From: Vitalii Fedusov Date: Sun, 30 Jul 2023 17:11:12 +0300 Subject: [PATCH] try add swipe reaction --- src/scripts/main.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/scripts/main.js b/src/scripts/main.js index 45f6e5b3f..efb91cb37 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -399,5 +399,44 @@ document.addEventListener('keydown', eventParam => { } }); +const gameTable = document.querySelector('.game-field'); + +let touchStartX = 0; +let touchStartY = 0; + +gameTable.addEventListener('touchstart', (eventPar) => { + touchStartX = eventPar.touches[0].clientX; + touchStartY = eventPar.touches[0].clientY; +}); + +gameTable.addEventListener('touchend', (eventPar) => { + const touchEndX = eventPar.changedTouches[0].clientX; + const touchEndY = eventPar.changedTouches[0].clientY; + const touchDiffX = touchEndX - touchStartX; + const touchDiffY = touchEndY - touchStartY; + + if (Math.abs(touchDiffX) > Math.abs(touchDiffY)) { + if (touchDiffX > 50) { + // Swipe right + moveRight(); + loseCheck(); + } else if (touchDiffX < -50) { + // Swipe left + moveLeft(); + loseCheck(); + } + } else { + if (touchDiffY > 50) { + // Swipe down + moveDown(); + loseCheck(); + } else if (touchDiffY < -50) { + // Swipe up + moveUp(); + loseCheck(); + } + } +}); + // document.addEventListener('keydown', event => console.log(event.key)); restart();