Fifth Tutorial: Balloon Popping game
Hello again! In the last tutorial, we talked about different methods of spawning objects on screen, and how to keep track of those objects. This time, we are going to talk about collisions, and what to do with them.
One of the many attractions of Corona SDK is the fact that physics comes built in, and there is a lot of logic that comes with a physics implementation. We have talked about physics a bit, but only as it pertains to the game world we have created. Now, we can talk about writing functions that DO SOMETHING when an object collides with another object. Collision functions can make things very interesting in our world, and certainly more interesting than they are already.
Let's revisit our code:
So, what we have above is our spawning function, which creates 5 balloons. You'll notice I modified the x and y coordinates a bit, so that the balloons are a bit spread out when they are created. Next, we have our touch listener function, which changes the colors of our objects when we click on them. Last but not least, we have our newly-created collision listener, which changes the color of the balloon that has affected the collision, and will print out the name of the balloon that has affected the collision. Be prepared to see a good amount of text printed to the terminal.
This is a very simple example of what can be done with a collision listener and objects. Let's get a bit more complex. I'm sure you all have been looking at the spawning code and thinking to yourselves "What's going on with that 'testBalloon1.id = i' bit?" Well, here's where it comes into play:
Now, every time our third balloon collides with anything, it changes color. You can still change it's color by clicking on it, but it will change back every time it collides with something. This makes things a bit more interesting, and all manner of functionality can be derived from this.
For more technical information on collisions, please see Corona's collision documentation here(http://developer.coronalabs.com/content/game-edition-collision-detection). It explains it far better than I ever could.
Next time we'll go over some info on graphics interaction and best practices for sprite functionality. See you then!