Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add event handlers for swipe actions on mobile devices #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="styles.css">
<script src="script.js" type="module"></script>
</head>
<body>
<div id="game-board"></div>
</body>
</html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2048</title>
<link rel="stylesheet" href="styles.css">
<script src="swiped-events.min.js"></script>
<script src="script.js" type="module"></script>
</head>
<body>
<div id="game-board"></div>
</body>
</html>
56 changes: 54 additions & 2 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,59 @@ grid.randomEmptyCell().tile = new Tile(gameBoard)
setupInput()

function setupInput() {
window.addEventListener("keydown", handleInput, { once: true })
window.addEventListener("keydown", handleInput, { once: true });
window.addEventListener("swiped", handleMobileInput, { once: true });
}

async function handleMobileInput(e) {
switch (e.detail.dir) {
case "up":
if (!canMoveUp()) {
setupInput();
return;
}
await moveUp();
break;
case "down":
e.preventDefault();
if (!canMoveDown()) {
setupInput();
return;
}
await moveDown();
break;
case "left":
if (!canMoveLeft()) {
setupInput();
return;
}
await moveLeft();
break;
case "right":
if (!canMoveRight()) {
setupInput();
return;
}
await moveRight();
break;
default:
setupInput();
return;
}

grid.cells.forEach((cell) => cell.mergeTiles());

const newTile = new Tile(gameBoard);
grid.randomEmptyCell().tile = newTile;

if (!canMoveUp() && !canMoveDown() && !canMoveLeft() && !canMoveRight()) {
newTile.waitForTransition(true).then(() => {
alert("Game Over!\nRefresh the page for a new one!");
});
return;
}

setupInput();
}

async function handleInput(e) {
Expand Down Expand Up @@ -54,7 +106,7 @@ async function handleInput(e) {

if (!canMoveUp() && !canMoveDown() && !canMoveLeft() && !canMoveRight()) {
newTile.waitForTransition(true).then(() => {
alert("You lose")
alert("Game Over!\nRefresh the page for a new one!");
})
return
}
Expand Down
7 changes: 6 additions & 1 deletion styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
font-family: Arial;
}

html,
body {
overscroll-behavior-y: contain;
}

body {
background-color: #333;
display: flex;
Expand Down Expand Up @@ -52,4 +57,4 @@ body {
opacity: .5;
transform: scale(0);
}
}
}
9 changes: 9 additions & 0 deletions swiped-events.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.