When making a game with phaser ce a common topic that comes up a lot is dealing with collision, there is detecting if a collision has occurred, and then there is doing something with that collision event. In this post I will be coving an examples of both using the default arcade physics engine in phaser ce. However the focus on this post will be on the body.onCollide event and how to use that to do something in the event of a collision.
This is a post on using the body.onCollide event with an instance of Phaser.signal. So in other words this is one of many posts on using the phaser ce default physics engine, and is not a getting started post on phaser ce. In this post I will be making use of many phaser ce features that I cover in other phaser ce posts of mine, I will try my best to link to other relevant content as needed.
For this basic example of body.onCollide I mad a quick project that involves a ball sprite that falls down onto a block, and when it hits the block it does damage to it. The block can be destroyed, and also re-spawn as well. Nothing to interesting but it will serve the purpose of demonstrating the use of the body.collide property just fine.
So I will want a method in which I create the ball that is to be used in the project. In this method I will be creating the sprite, enabling physics for it, and attach the on collide event. To do this I must first create an instance of Phaser.Signal and assign it to the body.onCollide property, this is to phaser knows that there is such an event set up for this ball sprite of mine. Once I have my Phaser.Signal instance I can attach a handler to it, in this handler I have access to the ball sprite, and the sprite that the ball has hit. For the sake of this example in the handler I just do damage to the block sprite.
So then I will also want a helper that ca be used to quickly create the block sprite that will be used for this example as well. Here I am not doing anything that special just creating the sprite, setting health for the sprite, and enabling physics for it as well. I will be setting the block as immovable, this will make it so that when the ball hits the block sprite that block sprite will not bounce back.
So I will want to also create some quick assets for the sprites as well. In place of using an external image I often use a canvas solution for making sprite sheets for thse kinds of projects.
So now that I have my helpers worked out it is time to use them with a Phaser.Game, and state Object. In the create method I call my helpers to set everything up, and also make a text object to display the status of the blocks health.
In the update method of the state object I check for collision between the ball and block. I also update the text on each tick, and check to see if the block has been destroyed if so I re spawn it.
So using body.onCollide is a great way to set up one or more events that are to be fired when a sprite collides with another sprite. It goes without saying that this can be very useful when working with most projects that involve sprites moving around an colliding with each other. I hope you found this post useful, and if you have anything that you would like to add, or have any questions be sure to let me know in the comments.