Weird Listener Behavior

Hello all!
I am switching between scenes using .start. The first time I switch to a scene, my listener runs once, which is expected. However, when I start the same scene again my listener runs twice, this would be expected but I’m using removeAllListeners. I’m pretty sure there’s something wrong with my code, but I can’t figure it out.

Here is my listener code:

this.input.keyboard.on('keydown-W', ()=>{
        console.log("test");
        // do stuff
});

and here is my switching code:

this.events.on('sleep', function() { // Remove pipeline
        console.log("sleeping");
        this.input.keyboard.removeAllListeners();
        this.game.renderer.removePipeline('pixelation' + date);
}.bind(this));

Here is what the console looks like:

// first start of scene

// w pressed once
test

// scene slept
sleeping

// scene started again

// w pressed once
test
test

Doing more testing shows that the number of scenes isn’t changing at all, but that could be an issue with my testing.

That’s the same problem I experienced before ‘desperate’ and creating my EventHandler to manage that.
The problem is, the event is not cleared as you expected. I dont know to manage it properly based on built-in phaser, maybe there is one out there (or some people that know will reply this as well).

But, in my case, you need to off ‘sleep’ event manually, right after want to re-start that scene. Or simply just use .once() instead .on() if its need to be fired once.

I expected this to work, but it didn’t. How should I move forward?

this.events.once('sleep', function() { // Remove pipeline
        //console.log(this.input.keyboard.removeAllListeners()); // remove all listeners isnt working as expected
        console.log(this.input.keyboard.off('keydown-W'));
        this.input.keyboard.off('keydown-S');
        this.input.keyboard.off('keydown-UP');
        this.input.keyboard.off('keydown-DOWN');
        this.game.renderer.removePipeline('pixelation' + date);
}.bind(this));

The solution for me is less of a solution and more of a compromise. Luckily for me, I only had one place where I switched scenes, so I just stopped using the ‘sleep’ listener and put my code right before the scene switch. After doing that, I didn’t get double listeners and everything worked fine.

Pseudocode-ish of my solution:

function changeScenes() {
    removePipeline();
    doOtherStuff();
    this.scene.switch('scene2');
}
1 Like