[SOLVED] Custom Event emitter communication across Scenes

Hi, I have a HUDScene and a LEVEL_1Scene. In a SceneController extends Phaser.Scene, i have a create() function which does this:

Blockquote
this.scene.start(CST.SCENES.LEVEL_1);
this.scene.launch(CST.SCENES.HUD);

I also have a controller.js class with constants for event emission. If i launch the HUDScene first, then start the LEVEL_1Scene, level1 event.emit isnt received by the HUD. However, if I start level 1 first, then lauch the HUD, everything works as planned.

Can anyone explain what is going on here? Or point me to an online explanation (other than Phaser 3 Docs and Notes, ive searched all over these sites, and havent found something like this addressed).

thanks in advance

I think we will need to see a bit more code in order to understand your problem.
What is your error, exactly? How do you pass the EventEmitter to your scenes? How do you emit/listen to events?

thanks for the reply telokis. i did a lot of actionscript 3 coding years ago, so i dont have a hard time with javascript, but i’m not really sure how event emitters work in phaser 3.

these are the first two lines in my create() function of each scene

Blockquote
emitter = new Phaser.Events.EventEmitter();

the emitter sends out info like this

Blockquote
emitter.emit(G.SCORE_UPDATED, 1);

and the listeners are something like this

Blockquote
emitter.on(G.SCORE_UPDATED, this.scoreUpdated, this);

event constants are in a seperate .js file.

if events are emitted by sceneA, and the listener is in sceneB, it’s a 50/50. sometimes they work, sometimes not so much.

so what i’m wondering first of all is, should i instantiate an emitter somewhere globally? would that be the proper way to go about it? or do i have to instantiate one in every scene?

okay, after flailing around with ES6, webpack, node, and singletons, i found out what my problem was.

the behavior was that sometimes events would be emitted by one scene and should have been received by another scene, at least thats what i thought. sometimes events were received, and sometimes not.

actually, all of my events werent being emitted by a scene at all, rather, sometimes i was trying to emit an event via a class object that didnt extend a scene.

once i put emitter references into the classes, they emit and receive just the way they should.

hope that helps someone else.

How did you add references into the classes?