Phaser GameObject Input Events

12/19/2020

Usually when you got pointer interaction with sprites, pointerevents come to mind. They're mouse and touch friendly plus they're familiar outside of the Phaser context. Less mentioned in tutorials but incredibly useful are the gameobjectevents. These events are fired for all interactive game objects in a scene.

This was useful for me when designing a simple menu scene. I wanted all the icons to increase in size if I hovered over them. The gameobjectover and gameobjectout events were exactly what I needed.

Generally, you can add a GameObject event listener like this:

function eventHandler(pointer, gameObject, event) {
  // Do stuff
};

this.input.on('gameobjectover', eventHandler);

I really like having the actual GameObject in the function as I'll have access to much more interesting properties than the pointer object can provide by itself. One thing to note, if you listening to a pointer event on a game object, that event will fire before this one fires.

Here's a quick example where I used the gameobject events to increase and decrease the size of a couple of sprites: https://repl.it/@MarcusSanatan/GameObject-Listeners.

Be sure to read the docs and see all possible events at your disposal.

Happy game deving!