Game Development Reference
In-Depth Information
bodyDef.type=b2Body.b2_dynamicBody;
bodyDef.userData="brick";
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);
}
2.
Then, obviously, let's give the birds something to kill. It's time to create
pigs, which are represented as circle shapes. There's nothing new in the pig
function, which just creates a circle given horizontal and vertical coordinates
and a radius, all in pixels.
private function pig(pX:int,pY:int,r:Number):void {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(pX/worldScale,pY/worldScale);
bodyDef.type=b2Body.b2_dynamicBody;
bodyDef.userData="pig"; var pigShape:b2CircleShape=
new b2CircleShape(r/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=pigShape;
fixtureDef.density=1;
fixtureDef.restitution=0.4;
fixtureDef.friction=0.5;
var thePig:b2Body=world.CreateBody(bodyDef);
thePig.CreateFixture(fixtureDef);
}
Also notice pig's custom data.
3.
In the Main function this time, we need to create a custom contact listener
because there are a lot of collisions to manage. Luckily, you already know
how to do it. Also, don't forget to create the pig.
public function Main() {
world=new b2World(new b2Vec2(0,5),true);
world.SetContactListener(new customContact());
debugDraw();
floor();
brick(402,431,140,36);
brick(544,431,140,36);
 
Search WWH ::




Custom Search