Game Development Reference
In-Depth Information
In the previous screenshot, the big brick cannot be destroyed if you click on
it, while the left totem "foot" can be destroyed. It's all a matter of storing and
retrieving the right data.
Anyway, setting and getting body user data is not the only way we have to check
information about our bodies.
At any time, we can scan through all bodies in the world and get their position and
rotation, as well as its speed and other useful information.
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.
1. First, we import the required classes to dynamically generate a text field and
give it some style:
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
2.
Then, a couple of new class level variables are added. textMon is the text
field itself, while textFormat is a TextFormat variable that will help us to
make the text field look prettier.
private var world:b2World;
private var worldScale:Number=30;
private var textMon:TextField = new TextField();
var textFormat:TextFormat = new TextFormat();
 
Search WWH ::




Custom Search