習作:Box2D
/**
* Copyright moriyoshi ( http://wonderfl.net/user/moriyoshi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/vUjL
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.text.*;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class FlashTest extends Sprite {
protected var world:b2World;
protected var zoomFactor:Number = 4.5;
protected var debugSprite:Sprite = null;
public function addBox(x:Number, y:Number, width:Number, height:Number, angle:Number):Array {
var itemdef:b2BodyDef = new b2BodyDef();
itemdef.position.Set(x, y);
itemdef.angle = angle;
var itemshape:b2PolygonDef = new b2PolygonDef();
itemshape.SetAsBox(width / 2, height / 2);
var itemsprite:Sprite = new Sprite();
with (itemsprite.graphics) {
beginFill(0xff000000);
var w:Number = zoomFactor * width / 2, h:Number = zoomFactor * height / 2;
drawRect(-w / 2, -h / 2, w, h);
endFill();
}
itemsprite.x = zoomFactor * x;
itemsprite.y = zoomFactor * y;
itemsprite.width = zoomFactor * width;
itemsprite.height = zoomFactor * height;
var item:b2Body = world.CreateBody(itemdef);
item.CreateShape(itemshape);
item.SetUserData(itemsprite);
addChild(itemsprite);
return [item, itemdef];
}
public function FlashTest() {
var aabb: b2AABB = new b2AABB();
aabb.lowerBound.Set(0., 0.);
aabb.upperBound.Set(100., 100.);
var g:b2Vec2 = new b2Vec2(0., 10.);
world = new b2World(aabb, g, true);
{
var text:TextField = new TextField();
var newFormat:TextFormat = new TextFormat();
newFormat.font = "明朝";
newFormat.size = 24;
text.defaultTextFormat = newFormat;
text.appendText("義務教育");
text.x = 40.;
text.y = 20.;
text.height = 50;
addChild(text);
}
if (false) {
debugSprite = new Sprite();
var debugDraw:b2DebugDraw = new b2DebugDraw();
debugDraw.m_sprite = debugSprite;
debugDraw.m_drawScale = zoomFactor;
debugDraw.m_fillAlpha = .0;
debugDraw.m_lineThickness = 1.;
debugDraw.m_drawFlags = 0xffffffff;
world.SetDebugDraw(debugDraw);
addChild(debugSprite);
}
var item:b2Body;
item = addBox(50., 90., 100., 5., 0.)[0];
with (item.GetShapeList()) {
m_friction = .3;
m_density = .0;
}
item.SetMassFromShapes();
item = addBox(2.5, 45., 5., 90., 0.)[0];
with (item.GetShapeList()) {
m_friction = .3;
m_density = .0;
}
item.SetMassFromShapes();
item = addBox(97.5, 45., 5., 90., 0.)[0];
with (item.GetShapeList()) {
m_friction = .3;
m_density = .0;
}
item.SetMassFromShapes();
for (var j:int = 0; j < 16; j++) {
item = addBox(Math.random() * 90 + 5., Math.random() * 50., 5., 5., Math.random() * Math.PI)[0];
with (item.GetShapeList()) {
m_friction = .3;
m_density = .5;
m_restitution = 1;
}
item.SetMassFromShapes();
}
addEventListener(Event.ENTER_FRAME, updateFrame, false, 0, true);
}
protected function updateFrame(ev:Event):void {
world.Step(1/10., 10);
for (var b:b2Body = world.GetBodyList(); b; b = b.GetNext()) {
if (b.GetUserData() is Sprite) {
var itemsprite:Sprite = b.GetUserData();
var pos:b2Vec2 = b.GetPosition();
itemsprite.x = pos.x * zoomFactor;
itemsprite.y = pos.y * zoomFactor;
itemsprite.rotation = b.GetAngle() * 180. / Math.PI;
}
}
}
}
}