Skip to content

Commit

Permalink
added swipe
Browse files Browse the repository at this point in the history
  • Loading branch information
cian0 committed Jul 20, 2024
1 parent 134e1cd commit 3e04686
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions emojiworld/emojigame.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Emoji Quest: Phaser 3 Edition</title>
<title>Responsive Emoji Quest: Phaser 3 Edition with Touch Controls</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
<style>
body {
Expand Down Expand Up @@ -118,6 +118,10 @@ <h1>Emoji Quest</h1>

generateMap(this, tileSize);
cursors = this.input.keyboard.createCursorKeys();

// Add touch input
this.input.on('pointerdown', onPointerDown, this);
this.input.on('pointerup', onPointerUp, this);
}

function generateMap(scene, tileSize) {
Expand Down Expand Up @@ -170,20 +174,50 @@ <h1>Emoji Quest</h1>
}
}

let touchStartX, touchStartY;

function onPointerDown(pointer) {
touchStartX = pointer.x;
touchStartY = pointer.y;
}

function onPointerUp(pointer) {
const touchEndX = pointer.x;
const touchEndY = pointer.y;
const dx = touchEndX - touchStartX;
const dy = touchEndY - touchStartY;
const absDx = Math.abs(dx);
const absDy = Math.abs(dy);

if (Math.max(absDx, absDy) > 50) { // Minimum swipe distance
if (absDx > absDy) {
// Horizontal swipe
player.body.setVelocityX(dx > 0 ? 200 : -200);
player.body.setVelocityY(0);
} else {
// Vertical swipe
player.body.setVelocityY(dy > 0 ? 200 : -200);
player.body.setVelocityX(0);
}
}
}

function update() {
const speed = 200;
player.body.setVelocity(0);

// Keyboard controls
if (cursors.left.isDown) {
player.body.setVelocityX(-speed);
player.body.setVelocityY(0);
} else if (cursors.right.isDown) {
player.body.setVelocityX(speed);
}

if (cursors.up.isDown) {
player.body.setVelocityY(0);
} else if (cursors.up.isDown) {
player.body.setVelocityY(-speed);
player.body.setVelocityX(0);
} else if (cursors.down.isDown) {
player.body.setVelocityY(speed);
player.body.setVelocityX(0);
}

moveMonster();
Expand Down

0 comments on commit 3e04686

Please sign in to comment.