Chapter 3. Interacting with Bodies
Every Box2D based game has its own way to interact with bodies. Totem Destroyer and Red Remover allow the player to destroy bodies by clicking them with the mouse, while Angry Birds makes bodies (birds) fly by dragging them. You already know how to create primitive and complex bodies; it's time to see how Box2D allows us to interact with bodies in its world.
In this chapter you will learn various ways to interact with and get information from Box2D bodies, including:
Selecting bodies with the mouse
Destroying bodies
Setting custom properties to bodies
Looping through all bodies in the world
Getting body information
By the end of the chapter, you will have a completely playable Totem Destroyer level.
The simplest and most intuitive way by which we can interact with Box2D bodies is destroying them with a mouse click.
Selecting and destroying bodies with a mouse click
We need to complete the Totem Destroyer prototype, so these concepts will be applied to the script you made in Chapter 2, Adding Bodies to the World. You should have a totem ready to be destroyed with an idol on top of it.
Before we see how to select and destroy bodies, we need to add a mouse click listener, so we need to import a new class to handle mouse events in our Main
class:
Then we can add the mouse listener in the Main
function:
Assigning custom attributes to bodies
Custom attributes can be of any type, but at the moment we'll just add a string: breakable
for breakable bricks, and unbreakable
for unbreakable bricks.
First, we'll be passing the string as an argument of the brick
function, so to reproduce the first level of Totem Destroyer we'll modify the Main
function in the following way:
And now let's have a look at how the brick
function changes:
Looping through bodies and getting their properties
The next example will build some kind of information display that monitors the idol. At every frame, we'll read the idol position, rotation, and speed. This way, you can assign events according to idol properties, such as giving a bonus if the idol did not fall down, or if it never reached a certain y-speed, or if it moved too far on the left, and so on. Knowing body properties is also very useful when you want to skin your game, as you can synchronize custom graphic assets to what happens in the Box2D World.
One step at time, let's start with our idol monitor. We are going to display idol data in a dynamic text field, so we need to make some basic changes to our class. I won't explain such changes as they are simple AS3 routines you should already know.
First, we import the required classes to dynamically generate a text field and give it some style:
Now you know how to select and destroy a body, as well as get body information. With these concepts you can build a complete, though not that advanced, Totem Destroyer prototype.
Although you aren't able to determine if the idol touched the ground (failing the level), you can check for its angle to be between -30 and +30 to show that the idol did not fall down, and create your first Totem Destroyer game.