Game Development Reference
In-Depth Information
3.
The beginning of this step is not that different from what you have already
done a thousand times through this topic. The Main function is shown
as follows:
public function Main() {
world=new b2World(new b2Vec2(0,5),true);
debugDraw();
ground();
var frontCart:b2Body=addCart(200,430);
var rearCart:b2Body=addCart(100,430);
addEventListener(Event.ENTER_FRAME,updateWorld);
}
4.
As usual we create a new world, a debug draw routine, a ground, and a
listener is added. Just to be sure to explain everything, the ground function,
which as usual creates a big static body to be used as a ground, is shown
as follows:
private function ground():void {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,470/worldScale);
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale,10/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
var groundBody:b2Body=world.CreateBody(bodyDef);
groundBody.CreateFixture(fixtureDef);
}
5.
The only new thing in the Main function is the addCart function, which just
adds a box shape at a given coordinate, so all in all there's nothing new yet:
private function addCart(pX:Number,pY:Number):b2Body {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(pX/worldScale,pY/worldScale);
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(40/worldScale,20/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.density=1;
fixtureDef.restitution=0.5;
fixtureDef.friction=0.5;
var body:b2Body=world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
var frontWheel:b2Body=addWheel(pX+20,pY+15);
var rearWheel:b2Body=addWheel(pX-20,pY+15);
return body;
}
 
Search WWH ::




Custom Search