Events are a big part of phaser ce game development, and the Phaser.Signal class is the phaser ce standard way of creating, and making use of events in phaser ce. There are many instances of Phaser.Signal to begin with in phaser that can be used to define some handers for when those events occur, but the class can also be used to define events as well. In this post I will be giving some use case examples of Phaser.Signal, including how to make one of my own, but I will not be covering all the different events that are built in.
In this post I am using phaser Community Edition 2.11.0 of phaser.
For this example I will be creating my own instance of Phaser.Signal to define my own event, as well as making use of a phaser ce built in event as well. This example will just be a sprite that falls down off screen, and when doing so will fire the phaser built in signal onOutOfBounds, doing so will result in the sprite taking damage, and will then start over at its starting location. Once the sprite looses all its health I will then use Signal.dispatch to fire mu own onGameOver event.
This helper when called will create my onGameOver event by calling Phaser.Signal with the new keyword. I can then save a reference to that signal somewhere such as to a game.data object. In this method I also add and event handler to the Signal with Signal.add
This helper will add an event handler to the onOutOfBounds event. The event will fire each time the sprite goes out of bounds, resulting in the sprite loosing health. I also check to see if the sprites health is completely gone, and if so fire my onGameOver event by calling the Signal.dispatch method that can be used to fire all handlers for the event.
Here I have a simple helper that will create the player sprite that will be used for this example. I set the health property here that will later be used with the damage method in my events above. For more information on why it might be a good idea to handle sprite health this way you can check out my post on sprite health and the damage method.
For this simple example I just quickly made a sprite sheet with canvas. If interested in reading more on how to go about making sprite sheets this way there is my post on making sprite sheets with canvas.
So the Phaser.Signal class is what is used with many built in events in phaser ce, and can also be used to create and trigger your own custom events as well. Next you might want to read more about the events component that contains many built in events for display objects such as sprites, and text.