Skip to content

Commit

Permalink
added demo for phaser 3 drag events
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Westover <[email protected]>
  • Loading branch information
scottwestover committed Jan 15, 2024
1 parent 3cbb439 commit 2e3ddc1
Show file tree
Hide file tree
Showing 10 changed files with 114,770 additions and 0 deletions.
3 changes: 3 additions & 0 deletions phaser-3/3.60/drag-game-objects/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["esbenp.prettier-vscode", "ritwickdey.LiveServer"]
}
12 changes: 12 additions & 0 deletions phaser-3/3.60/drag-game-objects/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"eslint.format.enable": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"prettier.singleQuote": true,
"prettier.semi": true,
"editor.formatOnSave": true,
"cSpell.words": [],
"prettier.printWidth": 120
}
13 changes: 13 additions & 0 deletions phaser-3/3.60/drag-game-objects/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Phaser 3 - Draggable Game Objects Demo

![demo](docs/example.gif)

A quick demo of how you can use the built in touch events of Phaser 3 to allow you to drag game objects in your Phaser 3 Scene.

For a detailed walkthrough, checkout my video on YouTube here:

Coming soon...

Link to live demo:

[Draggable Game Objects Demo](https://devshareacademy.github.io/code-examples-from-my-video-content/phaser-3/drag-game-objects/index.html)
Binary file added phaser-3/3.60/drag-game-objects/docs/example.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions phaser-3/3.60/drag-game-objects/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Phaser 3 - Drag Game Objects Demo</title>
<style>
html,
body {
margin: 0px;
}
</style>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script>
</head>
<body>
<div class="container" id="game-container"></div>
<script type="module" src="src/main.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions phaser-3/3.60/drag-game-objects/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "es6",
"target": "es6",
"checkJs": true
}
}
68 changes: 68 additions & 0 deletions phaser-3/3.60/drag-game-objects/src/draggable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import Phaser from './lib/phaser.js';

/**
* Uses the Phaser 3 built in drag events to allow a game object to be moved around a Phaser 3 Scene instance.
* The method will listen for the GameObject Destroy event and cleanup the various event listeners that
* were registered.
* @param {Phaser.GameObjects.Image | Phaser.GameObjects.Rectangle} gameObject
* @param {boolean} [enableLogs=false] enables logging for the various drag event callbacks. If the gameObject.name field
* is populated, this will be included in the log line.
*/
export function makeDraggable(gameObject, enableLogs = false) {
gameObject.setInteractive();

/**
* @param {string} message
* @returns {void}
*/
function log(message) {
if (enableLogs) {
console.debug(message);
}
}

/**
* @param {Phaser.Input.Pointer} pointer
* @returns {void}
*/
function onDrag(pointer) {
log(`[makeDraggable:onDrag] invoked for game object: ${gameObject.name}`);
gameObject.x = pointer.x;
gameObject.y = pointer.y;
}

/**
* @returns {void}
*/
function stopDrag() {
log(`[makeDraggable:stopDrag] invoked for game object: ${gameObject.name}`);
gameObject.on(Phaser.Input.Events.POINTER_DOWN, startDrag);
gameObject.off(Phaser.Input.Events.POINTER_MOVE, onDrag);
gameObject.off(Phaser.Input.Events.POINTER_UP, stopDrag);
gameObject.x = Math.round(gameObject.x);
gameObject.y = Math.round(gameObject.y);
}

/**
* @returns {void}
*/
function startDrag() {
log(`[makeDraggable:startDrag] invoked for game object: ${gameObject.name}`);
gameObject.off(Phaser.Input.Events.POINTER_DOWN, startDrag);
gameObject.on(Phaser.Input.Events.POINTER_MOVE, onDrag);
gameObject.on(Phaser.Input.Events.POINTER_UP, stopDrag);
}

/**
* @returns {void}
*/
function destroy() {
log(`[makeDraggable:destroy] invoked for game object: ${gameObject.name}`);
gameObject.off(Phaser.Input.Events.POINTER_DOWN, startDrag);
gameObject.off(Phaser.Input.Events.POINTER_MOVE, onDrag);
gameObject.off(Phaser.Input.Events.POINTER_UP, stopDrag);
}

gameObject.on(Phaser.Input.Events.POINTER_DOWN, startDrag);
gameObject.once(Phaser.GameObjects.Events.DESTROY, destroy);
}
1 change: 1 addition & 0 deletions phaser-3/3.60/drag-game-objects/src/lib/phaser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default window.Phaser;
38 changes: 38 additions & 0 deletions phaser-3/3.60/drag-game-objects/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { makeDraggable } from './draggable.js';
import Phaser from './lib/phaser.js';

class GameScene extends Phaser.Scene {
constructor() {
super(GameScene.name);
}

create() {
const rectangle = this.add.rectangle(this.scale.width / 2, this.scale.height / 2, 100, 100, 0xffff00);
makeDraggable(rectangle);
this.add
.text(this.scale.width / 2, 550, 'try dragging the rectangle around the scene', {
align: 'center',
fontSize: '22px',
wordWrap: {
width: this.scale.width - 50,
},
})
.setOrigin(0.5);
}
}

const gameConfig = {
type: Phaser.CANVAS,
pixelArt: true,
scale: {
parent: 'game-container',
width: 800,
height: 600,
autoCenter: Phaser.Scale.CENTER_BOTH,
mode: Phaser.Scale.FIT,
},
backgroundColor: '#5c5b5b',
scene: [GameScene],
};

const game = new Phaser.Game(gameConfig);
Loading

0 comments on commit 2e3ddc1

Please sign in to comment.