Game Development Reference
In-Depth Information
Placing the physics bird
This is when Box2D comes into play. As we are approximating the bird with a circle,
we need to create a Box2D circle in the same point from where the player released
the mouse.
Change the birdRelease function in the following way:
private function birdRelease(e:MouseEvent):void {
var sphereX:Number=theBird.x/worldScale;
var sphereY:Number=theBird.y/worldScale;
var r:Number = 15/worldScale;
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(sphereX,sphereY);
var circleShape:b2CircleShape=new b2CircleShape(r);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=circleShape;
fixtureDef.density=4;
fixtureDef.restitution=0.4;
fixtureDef.friction=0.5;
var physicsBird:b2Body=world.CreateBody(bodyDef);
physicsBird.CreateFixture(fixtureDef);
removeChild(theBird);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,birdMove);
stage.removeEventListener(MouseEvent.MOUSE_UP,birdRelease);
}
There's nothing new, we are only creating a static circle with the same size and in
the same coordinates of the graphic-only bird.
Test the movie by dragging and releasing the circle. It will turn into a static Box2D
circular body.
 
Search WWH ::




Custom Search