What the stopPropagation() means in this code example?

Hello, new noob here! I was looking at this example for swapping scenes: https://phaser.io/examples/v3/view/scenes/swapping-scenes

And I do not understand the idea behind this.input.stopPropagation(); called in the code.

What it does? Since there is no other input call, why stop propagation? What issue would happen if this code was not there?

https://photonstorm.github.io/phaser3-docs/Phaser.Input.InputPlugin.html#stopPropagation__anchor

That’s the docs entry for stopPropagation, it gives a pretty clear explanation for what it does.

This method should be called from within an input event handler, such as pointerdown
When called, it stops the Input Manager from allowing this specific event to be processed by any other Scene not yet handled in the scene list.

As for your question “What issue would happen if this code was not there?” you should just try it for yourself. The example you linked to has an “edit” button. Clicking it will bring you to an in-browser phaser editor for that example. Change the code to remove that call, run it, and see what’s different.

1 Like

I’m sorry, but I did both and still didn’t understand, that is why I came here.

Is the stopPropagation() call here used to stop the input from Scene A to also trigger something in Scene B? That is the purpose?

Also, I tried to edit and test it myself, but after saving and running… I saw no difference. I click and the scenes swap normally.

Yep, that is the purpose. When you have multiple scenes running parallel they all will receive input events. If you want to process that input in one scene and stop that event from propagating to the other scenes, that’s when you would call stopPropagation.

Oh, swap don’t pause/sleep the scene, so they are both happening at same time. So if I have a scene menu in front of my game scene, with swap and without stopPropagation, my clicks on the menu could trigger actions in the game itself, without me even seeing it. That is right?

This makes a lot of sense now. Sorry my slowness and thank you for the help!

1 Like