The awake event

We propose using Phaser events for implementing the User Components behaviors. However, the events provided in Phaser are not enough. When you create a component, all properties are set with the default values. Then, you set the value of each property. However, maybe you want to perform a custom initialization routine after all properties are set.

Looking into the Phaser events, you can do it in the first scene’s update. It means you can listen once for the UPDATE event and run the initialization routine. It may work in many cases. But maybe, you want to run this routine just after all properties are set, and before the game starts the update loop.

For this reason, the scene compiler generates code for emitting a custom event, the scene-awake event, just after it generates the code that creates the objects in the scene, and all their properties are set: .. code:

// code generated by the compiler:

editorCreate() {

    // creates the game object
    const dino = this.add.image(400, 240, "FufuSuperDino");

    // creates the PushOnClick component
    const dinoPushOnClick = new PushOnClick(dino);

    // sets the component's properties
    dinoPushOnClick.pushDelay = 500;

    // emit the scene-awake event, after all objects are created
    // and all properties are set
    dino.emit("scene-awake");
}

When you implement a component, you can register a listener on the scene-awake event:

class PushOnClick {

    constructor(gameObject) {
        ...

        this.gameObject = gameObject;

        gameObject.scene.events.once("scene-awake", () => {

            // I register a "pointerdown"
            // event for "animating" the game object
            // with a push effect

            // I set a pixel perfect handler
            // with certain alpha-tolerance
            const handler = this.scene.input
                .makePixelPerfect(this.alphaTolerance);


            this.gameObject.setInteractive(handler)
                .on("pointerdown", () => {
                    // animate the object with the push effect
                    this.gameObject.scene.add.tween(...);
                });
        });
    }

    /** @type {number} **/
    alphaTolerance = 100;
}

In the section A base class for your components), we explain how you can use a common super-class for all the components. It simplifies the listening of Phaser events, and it also includes the scene-awake event. So you can rewrite the previous PushOnClick component in this way:

class PushOnClick extends UserComponent {

    constructor(gameObject) {
        super(gameObject);
    }

    awake() {

        // Instead of registering a scene-awake event listener
        // you can override this method.

        ...

        this.gameObject.setInteractive(...)
                .on("pointerdown", ...);
        ...
    }
    ...
}

The scene-awake event is emitted in the editorCreate() method of a scene. So, if you create a new component and this component listens to the scene-awake event, then you should emit that event manually:

const comp = new PushOnClick(someSprite);
comp.alphaTolerance = 50;
// emit the awake event so the component can be notified
this.events.emit("scene-awake");

Emitting the scene-awake event later in the game is safe because components and prefabs handle this event only once in their lifetime.

The prefabs also listens to the scene-awake event. Learn more about it.