Game Development Reference
In-Depth Information
addChild(debugSprite);
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(worldScale);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
debugDraw.SetFillAlpha(0.5);
world.SetDebugDraw(debugDraw);
}
2.
Then, as we need to create a lot of bricks, a function to create a brick
starting from its position and size is strongly recommended, so here is a
brick
function with its arguments: brick horizontal and vertical origin,
width, and height.
private function brick(pX:int,pY:int,w:Number,h:Number):void {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(pX/worldScale,pY/worldScale);
// bodyDef.type=b2Body.b2_dynamicBody;
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(w/2/worldScale,h/2/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.density=2;
fixtureDef.restitution=0.4;
fixtureDef.friction=0.5;
var theBrick:b2Body=world.CreateBody(bodyDef);
theBrick.CreateFixture(fixtureDef);
}
Obviously, if you want you can also pass as arguments the density,
friction, and restitution.
If you look at the code, there's nothing new, but I want you to have a look at
the highlighted lines:
° First, the line that sets the
type
attribute is commented, so at the
moment we will create static bodies. When you are designing a level,
it is better to start with static bodies, and once you are satisfied, turn
them into dynamic bodies. Static bodies will help you to see the exact
location of each body, avoiding overlapping or other design mistakes.
° Second, in the
SetAsBox
attributes, the width and the height of the
brick are divided by two before they get divided by
worldScale
too.
I am doing this because I want to call the
brick
function with real
width and height in pixels, no matter if the
SetAsBox
method wants
the half-width and the half-height as you already know.