Game Development Reference
In-Depth Information
fixtureDef.shape=polygonShape;
fixtureDef.density=2;
fixtureDef.restitution=0.4;
fixtureDef.friction=0.5;
var theBrick:b2Body=world.CreateBody(bodyDef);
theBrick.CreateFixture(fixtureDef);
}
The string argument is called
s
and, as you can see when I created the body
definition, the
userData
property stores the string. Remember this property
as you will need it in most of your projects, whenever you have to store a
specific body data.
When we execute the
queryCallback
function after our bodies have their
data, we need to look for breakable string in our body user data.
We will destroy the body only if we find
breakable
, and optionally we can
output a message when we find
unbreakable
, such as "you can't destroy this
brick". But I am leaving this to you, as I just want to destroy the bricks.
3.
Change the
queryCallback
function in the following way:
private function queryCallback(fixture:b2Fixture):Boolean {
var touchedBody:b2Body=fixture.GetBody();
var userData:String=touchedBody.GetUserData();
if (userData=="breakable") {
world.DestroyBody(touchedBody);
}
return false;
}
You can read a body user data with the
GetUserData
method, so we just
need to check if we have found the
breakable
value.
4.
Test the movie and you will see you are only able to destroy breakable bricks:
![](Box2D-for-Flash-Games_images/img-gen149.png)
![](Box2D-for-Flash-Games_images/img-gen150.png)
![](Box2D-for-Flash-Games_images/img-gen151.png)