Jump to content

Help me understand: 3.13 scene shutdown with custom GameObjects


Recommended Posts


When Sprite's are created they are added to two lists within the Scene - the Display List and the Update List. Under 3.12 when a Scene was shut down it would emit a shutdown event, which Sprites listened out for. When they received it, they would destroy themselves.

After profiling and testing this process has changed slightly. Game Object's no longer listen for the Scene shutdown event. Instead, the Display List and Update List will iterate their children and call destroy on them in turn. If being destroyed by a Scene in this way, the child will skip several expensive operations in its destroy function. More importantly, in busy Scenes you no longer need thousands of event listeners registered. The result is that changing Scene is now up to 100% faster than before. You need not change your code to benefit from this, however, if you were relying on custom Game Objects listening for the Scene shutdown event natively, then this is no longer the case and you'll have to manually add that listener to your classes.


As I understand there was an optimization to how things are erased from old scenes (not active scenes). Considering the last sentence, I have to optimize my code. But I am not really aware of how. The shutdown event has to be called manually on custom GameObjects. But I only guess that's how to do:


class Parrot extends Phaser.GameObjects.Sprite {
	constructor(config) {
		super(config.scene, config.x * SCALE, config.y * SCALE, 'animals', 15);
        this.scene.events.on('shutdown', this.destroy(), this); //<- is it just that?

For any explanation I would be grateful, thanks in advance!


Link to comment
Share on other sites

  • 7 months later...

Also, keep in mind that in order to pass a function as argument to another function, you (usually*) must not invoke it.

When you wrote 

this.scene.events.on('shutdown', this.destroy(), this);

you basically called this.destroy() and gave this.scene.events.on whatever this.destroy() returned after being called.

That's like saying 'I want to add numbers 1 and 3 therefore I'm applying the operation "1+3" between them'. That's not correct since '1+3' is not an operation, but a result (4, to be precise). What you really wanted to say was 'I want to add numbers 1 and 3 therefore I'm applying the operation "+" between them'; therefore, analogically, the code above would simply change to

this.scene.events.on('shutdown', this.destroy, this);


*I said usually because it sometimes makes sense to invoke a function in another function's argument list when that function returns another function, i.e.

applyOperationBetweenNumbers(getOperationByName('plus'), 1, 3)


Link to comment
Share on other sites


  • Recently Browsing   0 members

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