SWIPING ON A MOBILE FONE OR touchscreen;)

This commit is contained in:
2024-08-10 15:41:09 +02:00
parent c0f11339fc
commit 6305cb0a0b
3 changed files with 67 additions and 85 deletions

View File

@@ -11,11 +11,5 @@
<h1>2048</h1> <h1>2048</h1>
<h2>Score: <span id="score">0</span></h2> <h2>Score: <span id="score">0</span></h2>
<div id="board"></div> <div id="board"></div>
<article class="arrowBack">
<button class="arrows" onclick="left()"><img src="" alt="Left Arrow"></button>
<button class="arrows" onclick="up()"><img src="" alt="Up Arrow"></button>
<button class="arrows" onclick="down()"><img src="" alt="Down Arrow"></button>
<button class="arrows" onclick="right()"><img src="" alt="Right Arrow"></button>
</article>
</body> </body>
</html> </html>

View File

@@ -2,6 +2,7 @@ var board;
var score = 0; var score = 0;
var rows = 4; var rows = 4;
var columns = 4; var columns = 4;
let startX, startY, endX, endY;
window.onload = function(){ window.onload = function(){
setGame(); setGame();
@@ -177,26 +178,42 @@ function hasEmptyTile() {
return false; return false;
} }
function left(){ document.addEventListener('touchstart', function(e){
startX = e.touches[0].clientX;
startY = e.touches[0].clientY;
}, {passive: true});
document.addEventListener('touchend', function(e){
endX = e.changedTouches[0].clientX;
endY = e.changedTouches[0].clientY;
handleSwipe();
}, {passive: true});
function handleSwipe(){
let diffX = endX - startX;
let diffY = endY - startY;
if(Math.abs(diffX)==Math.abs(diffY)){
console.log('Dot :3');
}else if(Math.abs(diffX) > Math.abs(diffY)){ //- Horizontal swipe
if(diffX > 0){ //? Right
slideRight();
setTwo();
document.getElementById("score").innerText = score;
}else{ //? Left
slideLeft(); slideLeft();
setTwo(); setTwo();
document.getElementById("score").innerText = score; document.getElementById("score").innerText = score;
} }
}else{ //- Vertical swipe
function up(){ if(diffY > 0){ //? Down
slideDown();
setTwo();
document.getElementById("score").innerText = score;
}else{ //? Up
slideUp(); slideUp();
setTwo(); setTwo();
document.getElementById("score").innerText = score; document.getElementById("score").innerText = score;
} }
function down(){
slideDown();
setTwo();
document.getElementById("score").innerText = score;
} }
function right(){
slideRight();
setTwo();
document.getElementById("score").innerText = score;
} }

View File

@@ -19,10 +19,6 @@ body{
background-size: cover; background-size: cover;
} }
hr{
width: 500px;
}
#board{ #board{
width: 372px; width: 372px;
height: 372px; height: 372px;
@@ -65,23 +61,6 @@ hr{
} }
} }
.arrowBack{
height: 90px;
width: 372px;
display: flex;
flex-direction: row;
justify-content: space-between;
margin-top: 1rem;
}
.arrows{
height: 90px;
width: 90px;
border: none;
border-radius: 50%;
}
@media screen and (max-width: 500px){ @media screen and (max-width: 500px){
body{ body{
background-image: url("./mobile.jpg"); background-image: url("./mobile.jpg");
@@ -96,14 +75,6 @@ hr{
width: 60px; width: 60px;
border: 3px solid #bbada0; border: 3px solid #bbada0;
} }
.arrowBack{
height: 60px;
width: 246px;
}
.arrows{
height: 60px;
width: 60px;
}
} }
.x2{ .x2{