Jump to content

Events / Signals


mcolman
 Share

Recommended Posts

Hey guys,

 

Phaser newbie here, need some help with events.

 

How are people dispatching custom events throughout their games. 

 

Let's say for example you have 2 classes. Main and Car. Main adds the Car class.

 

The Main class needs to know when the car is low on petrol to update the UI. 

 

So would you use Phaser.Signals to handle this communication, or would you just call a function in the Main class directly from the Car class?

 

Are there plans to improve events in Phaser? 

 

I understand events can't be global to the Phaser namespace because we need to support multiple Phaser instances, but couldn't events exist globally inside each World instance? All display objects within the World could dispatch and listen for events. Events could bubble up the display list. Does this sound feasible?

 

Thanks!

 

Matt. 

Link to comment
Share on other sites

You'd use a signal in the same way you'd use an event traditionally. They're just not bound to strings that's all.

 

In Car:

lowFuel = new Phaser.Signal();...function carRunningLow() {  lowFuel.dispatch(this, fuelLeftVar);}

In Main:

car.lowFuel.add(this.refuel, this);function refuel(car, fuel) {  // do something}

A single listener can listen for signals from any object, it's not a 1:1 relationship.

 

Signal.dispatch can dispatch anything as its arguments. In the example above I sent a reference to the Car object and a 'fuelLeft' variable, but it could be anything (or indeed nothing).

 

Signals are much more powerful than traditional events imho, which is why Phaser uses them. They're just unfamiliar to those more used to JS events.

Link to comment
Share on other sites

  • 1 year later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...