Can you please show me how the dom can communicate mouse pointer movement events as an input to a phaser scene. I've seen how mouse movement can be tracked within the scene area; but once the mouse leaves and goes to other areas of the DOM, that data is not tracked. I figure if perhaps there was a way to have an input to communicate data from "the outside world" then this could be possible. I am very grateful for help and direction you could share.
Asked
Active
Viewed 1,070 times
1 Answers
1
All you need to do is add an event listener to the DOM object you also want to track movement in using plain JavaScript. Then, you tie the event listener to the game method you want to execute when the event is triggered.
const body = document.querySelector('body');
body.onmousemove = (pointer) => {
updatePoint(pointer);
};
And then setup your game as normal:
const config = {
type: Phaser.CANVAS,
height: 400,
width: 400,
parent: 'gameContainer',
scene: {
create: create
}
};
const game = new Phaser.Game(config);
let dataText;
function create() {
this.input.on('pointermove', (pointer) => {
updatePoint(pointer);
});
dataText = this.add.text (10, 10, 'x, y');
}
function updatePoint(pointer) {
dataText.text = 'x: ' + pointer.offsetX + ', y: ' + pointer.offsetY;
}
You may have to refactor your code a little bit to get this to work, because the event listener on your DOM element needs to be able to access the game methods. I created a quick codepen showing the setup that worked for me.

brae
- 1,052
- 8
- 18
-
thank you so much for your help! In what ways can this input update/coordinate with this.input.activePointer? Also, this isn't explicit composition; Is there a way to have the curser (pointer) location variable (dataText) used as a constructor argument value when a scene in my GameObj instantiates a new Object from a class that extends Phaser.GameObjects.Graphics/Image/ect... ? ie const cBub = new CoordinatesBubble({ location: dataText}) - - - - what i don't understand is how the child changes upon mousemove - - - - (Similar to how angular handles dynamic values used as @Input for components) – Benjamin McFerren Oct 11 '19 at 13:44
-
I'm having trouble understanding what additional help you're asking for. Could you update the original question with your code samples and what you're trying to do? Maybe then I can help you better. – brae Oct 11 '19 at 14:43
-
these links might help explain: https://stackoverflow.com/questions/45749533/what-is-input-used-for - - - https://www.w3schools.com/js/js_object_constructors.asp - - - https://github.com/Polymer/lit-html/issues/877 – Benjamin McFerren Oct 11 '19 at 15:24
-
No, not really... The only way I can provide additional clarification is if I understand what you're trying to do related to this question. – brae Oct 14 '19 at 12:18