Setting an out of bounds event in phaser ce
1 - What to know
2 - The onOutOfBounds example
For this example I have a project that creates a pool of enemy sprites that will spawn from the bottom of the screen, and then move up to the top of the screen. Once they go out of bounds after reaching the top the onOutOfBounds event will fire resulting in the player losing health, and the enemy being killed which will case it to potentially be reused.
2.1 - The onOutOfBounds handler
This is the handler that will fire each time an enemy reaches the top of the screen, here I check that the y value is indeed less than zero, and so set its y value back to a starting y value, as well as deduct health from the player, and preform other necessary actions.
So now that I have my handler I will need to attach it to one or more sprites that make use of it, so I will want a method that will create a pool of enemies and within there I will attach this handler for each sprite.
2.2 - Create a pool of enemies
Here I have a method that I can use to create a pool of enemies. For each enemy I attach my onOutOfBounds hander by attaching it via enemy.events.onOutOfBounds, but first I make sure to set the checkWorldBounds boolean to true as well or else the event will not fire. In this method I also do whatever else is called for each child in the enemies sprite pool.
All the enemies will start of as dead by calling the kill command, this will not destroy the sprite, but place it in a dead state. I will then have a spawn method that will be called every so often that will revive the dead sprites at which point they will start to cross the screen.
2.3 - The Enemy Spawn method
Here Is a method that I will call with a timer every once in a while to revive dead sprites, and cause them to start to cross the screen.
It would seem that there is no getRandomDead method built into phaser so I had to use Group.filter to filter out enemies that are alive, and then spawn a random index from that list so that they are not released in a predictable pattern sense I am keeping the x values of the sprites fixed.
2.4 - The sprite sheet
For this example I made a sprite sheet using canvas
2.5 - The Phaser.game instance
Dow it is time to make the magic happen with an instance of Phaser.Game and a state object. Here I am using the helper methods that I have wrote about above, and well as setting up a game.data object that I often use as a way to store variables that will be used across the whole project.
In the update method I am also using game.time.elapse as a way to have the sprites move over time by a pixel per second rate as well.
3 - Conclusion
So the onOutOfbounds event is great for attaching logic to a sprite that is to be called when the sprite goes out of bounds. For example if making a breakout style game I can attach an event that will result in the player loosing a live when the ball goes out of bounds by falling bellow the game height. If you have any questions, or would like for me to add more to this post for whatever reason be sure to let me know in the comments. Thank you for reading and have fun with phaser!