Game Development Reference
In-Depth Information
9.
At this time, two static carts will be created. Test the movie and check if
everything seems ok:
Remember, when you're building something new with Box2D,
always start with static bodies to what your level is going to look,
with no gravity, forces, and collisions.
Now that everything looks fine, let's make bodies dynamic and add
required joints.
10. Start making wheels and carts dynamic by adding the
type
property in
both
addWheel
and
addCart
functions. The
addWheel
function is shown
as follows:
private function addWheel(pX:Number,pY:Number):b2Body {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(pX/worldScale,pY/worldScale);
bodyDef.type=b2Body.b2_dynamicBody;
var circleShape:b2CircleShape=new b2CircleShape(0.5);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=circleShape;
fixtureDef.density=1;
fixtureDef.restitution=0.5;
fixtureDef.friction=0.5;
var body:b2Body=world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
return body;
}
11. And the
addCart
function must be modified in the same way:
private function addCart(pX:Number,pY:Number):b2Body {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(pX/worldScale,pY/worldScale);