Jump to content

Event for when game objects are added/removed?


Max
 Share

Recommended Posts

Hi everybody,

for my game, I would like to do something whenever a game object gets created / dies and is destroyed. If there was something like game.on('create:game-object', cb); or something similar, that would be exactly what I'm looking for.

I couldn't find anything in the docs, is this kind of thing supported by Phaser? If not, what are common alternatives to this kind of thing?

Thanks for your help! :)

Link to comment
Share on other sites

There are a number of events you can listen for:

this.onAddedToGroup = new Phaser.Signal();    this.onRemovedFromGroup = new Phaser.Signal();    this.onKilled = new Phaser.Signal();    this.onRevived = new Phaser.Signal();    this.onOutOfBounds = new Phaser.Signal();    this.onEnterBounds = new Phaser.Signal();    this.onInputOver = null;    this.onInputOut = null;    this.onInputDown = null;    this.onInputUp = null;    this.onDragStart = null;    this.onDragStop = null;    this.onAnimationStart = null;    this.onAnimationComplete = null;    this.onAnimationLoop = null;
Link to comment
Share on other sites

The Input events are created if you do Sprite.inputEnabled = true. The Animation events are created if you define an animation on your Sprite.

 

You get to all of them via Sprite.events, so:

var bob = game.add.sprite(100, 100, 'spaceship');bob.events.onKilled.add(bobDied, this);function bobDied() {// mourn the loss of bob}
Link to comment
Share on other sites

There are a number of events you can listen for[...]

Thank you for your help Rich! I saw those events in the docs but you bind them on individual sprites, right? I'm looking to bind to an event emitter once that notifies me whenever any game object gets added to the game / dies / gets removed. I don't want to bind on individual sprites and I need to be notified when one is added to the game (not just instantiated). Is this possible in Phaser?

Link to comment
Share on other sites

Nope, I had considered adding it as a Group level event (as the Game is technically a Group) but it's just a lot of noise and conditional checks I didn't want to be making at the time (and even then it wouldn't catch a sprite being added as a child of another sprite). It could be done by extending Group with your own class and over-riding the 'add' method in there, making it dispatch something when called. Then just add the Group to the World as the only thing on the display list (at that level).

Link to comment
Share on other sites

Nope, I had considered adding it as a Group level event (as the Game is technically a Group) but it's just a lot of noise and conditional checks I didn't want to be making at the time (and even then it wouldn't catch a sprite being added as a child of another sprite). It could be done by extending Group with your own class and over-riding the 'add' method in there, making it dispatch something when called. Then just add the Group to the World as the only thing on the display list (at that level).

OK, thanks for clearing that up. I'll have a look at that, thanks! :)

Link to comment
Share on other sites

  • 1 year later...

I know this is a rather old post, but I encountered the same issue and resolved it by extending Phaser.Group and overriding the add method as follows. 

add(powerup) {        powerup.events.onAddedToGroup.addOnce(() => {            this.width += powerup.width;            powerup.y = 0;            powerup.x = this.width;        });        powerup.events.onRemovedFromGroup.addOnce(() => {            this.width -= powerup.width;        });        super.add(powerup);} 

Of course, you would have to add these hooks into any add methods (could create group wrapper that does this), but at least you don't have to wrap the removed ones! Native events for this would be nice though
 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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