Here is the source code that you will end up with once you have finished this tutorial. In the future, and as the tutorials get bigger, I'll get a github account to upload to otherwise my blogs are going to get even longer :O.
package
{
import Box2D.Collision.Shapes.b2CircleShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* @author Sp Games
*/
public class Main extends Sprite
{
public var world_scale:Number = 30;
public var world:b2World = new b2World(new b2Vec2(0, 9.8), true);
public function Main():void
{
addEventListener(Event.ENTER_FRAME, update);
add_circle();
debug_draw();
}
public function add_circle():void
{
var my_body:b2BodyDef = new b2BodyDef();
my_body.position.Set((stage.stageWidth / 2) / world_scale,
(stage.stageHeight / 2) / world_scale);
my_body.type = b2Body.b2_dynamicBody;
var my_circle:b2CircleShape = new b2CircleShape(10 / world_scale);
var my_fixture:b2FixtureDef = new b2FixtureDef();
my_fixture.shape = my_circle;
my_fixture.density = 1.0;
my_fixture.friction = 1;
var body:b2Body = world.CreateBody(my_body);
body.CreateFixture(my_fixture);
}
public function debug_draw():void {
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.SetSprite(debug_sprite);
debug_draw.SetDrawScale(world_scale);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
world.SetDebugDraw(debug_draw);
}
private function update(e:Event):void
{
world.Step(1 / 30, 10, 10);
world.ClearForces();
world.DrawDebugData();
}
}
}

0 comments:
Post a Comment