Game Development Reference
In-Depth Information
3.
The following is how the functions in your Main class will look now:
public function Main() {
world=new b2World(new b2Vec2(0,5),true);
debugDraw();
// level design goes here
addEventListener(Event.ENTER_FRAME,updateWorld);
}
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);
}
private function debugDraw():void {
var debugDraw:b2DebugDraw=new b2DebugDraw();
var debugSprite:Sprite=new Sprite();
addChild(debugSprite);
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(worldScale);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
debugDraw.SetFillAlpha(0.5);
world.SetDebugDraw(debugDraw);
}
private function updateWorld(e:Event):void {
world.Step(1/30,10,10);
world.ClearForces();
world.DrawDebugData();
}
Now the Main function is simple and clear, and we can build our level just by
calling the brick function six times, one per block.
Also, notice how I have set the gravity to (0,5) rather than real-world
gravity as in the falling ball example. A weaker gravity will make the totem
destroy and fall down slowly, with a dramatic effect. Anyway, it's just a
game-design choice, and you're free to set your own gravity.
 
Search WWH ::




Custom Search