Using Event Emitters to communicate between Scenes

Hello,

I have a Scene 1 and a Scene 2. I am trying to use an event emitter to pass an integer variable called “playerShip” from scene 1 to scene 2, and then call a function called chooseShip() with that int variable as a parameter. Here is the related code so far:

Scene 1:

`this.emitter = new Emitter();

this.emitter.addListener(‘update-ship’, this.chooseShip, playerShip);

this.playerShip = 1;

eventsCenter.emit(‘update-ship’, this.playerShip);`

Scene 2:
this.scene.get('bootGame').events.on('update-ship', this.chooseShip, this);

bootGame is the label for Scene 1.

Right now I’m getting an error “Emitter is not defined,” referring to when I tried to create an emitter object in scene 1. This may be because I haven’t imported anything, but I’m not sure how to do that either.

I think this is doing what you want.

I tried this method before I tried what I was doing now, but the error I was getting was that all my “eventsCenter” stuff was undefined. I think it’s because I couldn’t find a place to put the import statement " import eventsCenter from './EventsCenter'". Do you happen to know where this is supposed to go?

This was the post I was consulting for this method. Do you happen to know if we’re supposed to import the Emitter? if so how?

No. This is all you need.

// level scene
this.events.emit('upPoints' /*…*/);

// HUD scene
this.scene.get('Level_One').events.on('upPoints' /*…*/);
this.emitter = new Phaser.Events.EventEmitter();

http://phaser.io/examples/v3/view/events/create-event-emitter