Heya,
I’m having some trouble with gameObjects events, I’m trying to have multiple gameObjects of type Unit in one scene, which are supposed to be individually hoverable and selectable. I make sure every GO is interactive, and add the event listeners on the GO itself. The issue is that only the first rendered Unit is actually responsive (in this case it’s the ‘orc’) and the others won’t behave as expected with this.on(‘pointerover’,…). It’s still possible to click them (‘pointerdown’ event), but not possible to use ‘pointover’.
Things get more complicated if I try to change the hitArea - the hover works as expected only for the first Unit gameObject, but works unexpectedly when clicking inside the sprite bounds but outside the hitArea set (always a rectangle) and it does count it as a click.
What are some possible approaches to solve this?
This is the Unit class:
import Phaser from 'phaser';
export class Unit extends Phaser.GameObjects.Sprite {
constructor(config) {
super(config.scene, tileX, tileY, config.key);
this.scene = config.scene;
this.key = config.key;
this.makeSelectable();
this.scene.add.existing(this);
}
makeSelectable(){
this.setInteractive();
this.selected = false;
this.on('pointerover',()=>{
if (!this.selected){
this.tint(0x00FF00);
}
})
this.on('pointerout',()=>{
if (!this.selected){
this.tint(0x000000);
}
this.on('pointerdown',()=>{
this.selected = true;
this.tint(0xFFFFFF);
})
}
the creation of the units:
let units = [];
units.push ( new Unit({
scene: this,
x: 47,
y: 34,
key: 'orc'
}))
units.push (new Unit({
scene: this,
x: 32,
y: 15,
key: 'human'
}))