-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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) { | ||
|
@@ -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(); | ||
|