diff --git a/Block Down Game/assets/js/main.js b/Block Down Game/assets/js/main.js index 30dbe97..f29b8cb 100644 --- a/Block Down Game/assets/js/main.js +++ b/Block Down Game/assets/js/main.js @@ -22,7 +22,30 @@ document.addEventListener('keydown', (event) => { moveLeft() } else if (event.key == "ArrowRight") moveRight(); -}) +}) + +let touchStartX = 0; +let touchEndX = 0; + +document.addEventListener('touchstart', (event) => { + touchStartX = event.touches[0].clientX; + touchEndX = touchStartX; // Initialize touchEndX to the same value as touchStartX +}); + +document.addEventListener('touchmove', (event) => { + touchEndX = event.touches[0].clientX; +}); + +document.addEventListener('touchend', () => { + const swipeDistance = touchEndX - touchStartX; + if (swipeDistance > 50) { + moveRight(); // Handle right swipe + } else if (swipeDistance < -50) { + moveLeft(); // Handle left swipe + } +}); + + block.addEventListener('animationiteration', () => { const randPos = Math.floor((Math.random() * 3)) * 100;