Game Development Reference
In-Depth Information
Look at the previous screenshot. On the left, I destroyed the floor, which is wrong.
On the right, I destroyed the upper big brick, which can't be destroyed in the
original game.
That's quite an issue, so we must find a way to tell Box2D which bodies can be
destroyed and which bodies cannot.
Luckily, Box2D body definition management is so advanced that we can even add
our custom attributes to a body.
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.
1.
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:
public function Main() {
world=new b2World(new b2Vec2(0,5),true);
debugDraw();
brick(275,435,30,30,"breakable");
brick(365,435,30,30,"breakable");
brick(320,405,120,30,"breakable");
brick(320,375,60,30,"unbreakable");
brick(305,345,90,30,"breakable");
brick(320,300,120,60,"unbreakable");
idol(320,242);
floor();
addEventListener(Event.ENTER_FRAME,updateWorld);
stage.addEventListener(MouseEvent.CLICK,destroyBrick);
}
2.
And now let's have a look at how the brick function changes:
private function
brick(pX:int,pY:int,w:Number,h:Number,s:String):void {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(pX/worldScale,pY/worldScale);
bodyDef.type=b2Body.b2_dynamicBody;
bodyDef.userData=s;
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(w/2/worldScale,h/2/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
 
Search WWH ::




Custom Search