mcolman Posted September 7, 2015 Share Posted September 7, 2015 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 More sharing options...
rich Posted September 7, 2015 Share Posted September 7, 2015 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. Tilde, DawnPaladin and in mono 3 Link to comment Share on other sites More sharing options...
mcolman Posted September 7, 2015 Author Share Posted September 7, 2015 ok great! Thanks @rich! I might PR something like this to the examples page.I was also questioning the use of Signals because I thought I saw you mention in a Phaser 3 post that you want to get rid of Signals? Link to comment Share on other sites More sharing options...
rich Posted September 7, 2015 Share Posted September 7, 2015 Yes I'm considering it, but only because I get lots of posts like yours quiphop 1 Link to comment Share on other sites More sharing options...
qdrj Posted September 7, 2015 Share Posted September 7, 2015 Yes I'm considering it, but only because I get lots of posts like yours What will be replacement for Signals? Link to comment Share on other sites More sharing options...
Bespired Posted April 1, 2017 Share Posted April 1, 2017 jQuery got: $(document).on('custom-event', function(evt,data){ }); $(document).trigger('custom-event', {data: 'hello'}); Would be nice to build Phaser games without jQuery. Link to comment Share on other sites More sharing options...
Recommended Posts