flash on 2009-10-14 box2d test
/**
* Copyright sowcodWonderfl ( http://wonderfl.net/user/sowcodWonderfl )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tuGp
*/
package {
import flash.display.*;
import Box2D.Dynamics.*;
import Box2D.Common.Math.*;
import Box2D.Collision.Shapes.*;
import flash.events.*;
[SWF(backgroundColor="#000000", frameRate="60")]
public class FlashTest extends Sprite {
private var field:MyWorld;
private var drawer:MyDrawer;
private var overlay:Sprite;
public function FlashTest() {
// write as3 code here..
this.field = new MyWorld();
this.drawer = new MyDrawer(this,this.field);
this.addChild(this.drawer);
this.overlay = new Sprite();
this.addChild(this.overlay);
this.createBodys();
this.addEventListener(Event.ENTER_FRAME,this.onEnterFrame);
}
private function onEnterFrame(ev:Event):void {
/*
var g:Graphics = this.overlay.graphics;
g.clear();
g.lineStyle(1,0x000000);
g.moveTo(0,0);
g.lineTo(Math.random()*100,Math.random()*100);
*/
field.world.Step(1.0/9.0, 1);
}
private function createBodys():void {
createGround();
for(var i:Number = 0; i<50; ++i){
createBallBody(new b2Vec2(200,Math.floor(100+Math.random()*4-2)),10);
}
}
private function createBallBody(position:b2Vec2,radius:Number):b2Body {
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(position.x,position.y);
var body:b2Body = field.world.CreateBody(bodyDef);
var shapeDef:b2CircleDef = new b2CircleDef();
shapeDef.radius = radius;
shapeDef.density = 1.0;
shapeDef.restitution = 0.4;
shapeDef.friction = 0.3;
body.CreateShape(shapeDef);
body.SetMassFromShapes();
return body;
}
private function createGround():b2Body {
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(220,400);
var body:b2Body = field.world.CreateBody(bodyDef);
var shapeDef:b2PolygonDef = new b2PolygonDef();
shapeDef.SetAsBox(200,30);
shapeDef.friction = 0.8;
body.CreateShape(shapeDef);
return body;
}
}
}
import Box2D.Dynamics.*;
import Box2D.Collision.b2AABB;
import Box2D.Common.Math.b2Vec2;
import flash.display.*;
class MyWorld {
private var worldAABB:b2AABB;
public var world:b2World;
public function MyWorld() {
this.worldAABB = new b2AABB();
this.worldAABB.lowerBound.Set(0,-300);
this.worldAABB.upperBound.Set(500,500);
var gravity:b2Vec2 = new b2Vec2(0,10.0);
this.world = new b2World(worldAABB,gravity,true);
}
}
class MyDrawer extends Sprite{
private var drawTarget:Sprite;
private var field:MyWorld;
public function MyDrawer(drawTarget:Sprite,field:MyWorld) {
this.drawTarget = this;
this.field = field;
setting();
}
private function setting():void {
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.m_sprite = drawTarget;
debugDraw.m_drawScale = 1.0;
debugDraw.m_lineThickness = 1;
debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit;
field.world.SetDebugDraw(debugDraw);
}
}