Multiple event firings on single pointer up event

Hello! First post here on the Phaser forums. Hoping some experienced folk can guide me on an issue I am having.

I’ve encountered a pretty confusing issue with a pointerup event I have created. I only intend for it to fire once (the use case is user clicks an animated sprite and this opens a menu). At first this worked great, only firing once. But now it is firing multiple times (sometimes up to seven or more times) on a single click. Even stranger is sometimes the issue goes away, only firing once, only to later start acting up again.

My guesses are that it has something to do with it being an animated sprite (animating over 6 frames split out from one sprite sheet), perhaps it is firing the event for each frame? The number of events do not match to the number of frames, but its my best guess as to what could be causing the issue. Anyway, here is some of my code.


this.load.spritesheet('recruiter-idle', './assets/sprites/tavern/tavern-recruiter.png', { frameWidth: 32, frameHeight: 64 });

let recruiter = this.add.sprite(300, 360, 'recruiter-idle', 0);

recruiter.on('pointerup', () => { // The part firing several times, only needs to fire once.
      EventBus.emit('entity-clicked', 'recruiter');
});

this.anims.create({
      key: "recruiter-idle",
      frames: this.anims.generateFrameNumbers('recruiter-idle', { start: 0, end: 5 }),
      frameRate: 3,
      repeat: -1
    });

recruiter.play('recruiter-idle');

Happy to provide more detail or code if it helps. Thanks all!

EDIT: I found in some additional testing that this appears to be happening due to exiting the page and returning. To provide more detail, the phaser scene lives within a single page app. When I route to a view and return, it appears to create another listener. So on first visit it correctly has one event, with one more added each time.

So my new question is, how can I destroy all of the current active events so that there are no pre existing ones when I visit the page again? Thanks!

When is this called? What function is it within?

This is called from create() within my scene class.

It may be EventBus that needs to be cleared. A scene’s input handlers are cleared automatically when it shuts down.

Add a console log inside the “pointerdown” handler to see if that’s really getting repeated.

1 Like

The EventBus was the culprit! Thank you for pointing out the obvious to me. Sometimes it takes another pair of eyes! Appreciate it.

1 Like