# Weird Listener Behavior

**URL:** <https://phaser.discourse.group/t/weird-listener-behavior/5970>\
**Category:** Phaser 3\
**Created:** [April 19, 2020, 12:41am UTC](https://phaser.discourse.group/t/weird-listener-behavior/5970 "2020-04-19T00:41:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 19, 2020, 12:41am UTC](https://phaser.discourse.group/t/weird-listener-behavior/5970/1 "2020-04-19T00:41:29Z")

</div>

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:

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

```

and here is my switching code:

```javascript
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:

```javascript
// 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.

---

<div class="post-metadata">

**Author:** ![ReydVires](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/reydvires/32/939_2.png) [@ReydVires](https://phaser.discourse.group/u/ReydVires)\
**Post date:** [April 19, 2020, 1:27am UTC](https://phaser.discourse.group/t/weird-listener-behavior/5970/2 "2020-04-19T01:27:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 19, 2020, 12:15pm UTC](https://phaser.discourse.group/t/weird-listener-behavior/5970/3 "2020-04-19T12:15:13Z")

</div>

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

```javascript
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));

```

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 19, 2020, 2:58pm UTC](https://phaser.discourse.group/t/weird-listener-behavior/5970/4 "2020-04-19T14:58:32Z")

</div>

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:

```javascript
function changeScenes() {
    removePipeline();
    doOtherStuff();
    this.scene.switch('scene2');
}

```
