package{
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Collision.Shapes.b2Shape;
import Box2D.Collision.b2AABB;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.Joints.b2MouseJoint;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2DebugDraw;
import Box2D.Dynamics.b2Fixture;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width=”800″,height=”600″,frameRate=”32″,backgroundColor=”0x000000″)]
public class UnitAndSelect2 extends Sprite{
private var world:b2World;
private var body:b2Body;
private var timeStep:Number;
private var iterations:uint;
private var pixelsPerMeter:Number = 30;
private var mouseJoint:b2MouseJoint;
private var mousePVec:b2Vec2 = new b2Vec2();
[Embed(source=”assets/assets.swf”,symbol=”floor”)]
private var floor:Class;
[Embed(source=”assets/assets.swf”,symbol=”crate”)]
private var crate:Class;
public function UnitAndSelect2(){
createWorld();
makeDebugDraw();
createWall();
makeBodies();
addEventListener(Event.ENTER_FRAME,onEnterframe);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
}
private function createWorld():void{
var gravity:b2Vec2 = new b2Vec2(0.0,9.8);
var doSleep:Boolean = true;
world = new b2World(gravity,doSleep);
world.SetWarmStarting(true);
timeStep = 1.0/30.0;
iterations = 10;
}
private function makeDebugDraw():void{
var debugDraw:b2DebugDraw = new b2DebugDraw();
var debugSprite:Sprite = new Sprite();
addChild(debugSprite);
debugDraw.SetSprite(debugSprite);
debugDraw.SetDrawScale(30.0);
debugDraw.SetFillAlpha(0.5);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
}
private function createWall():void{
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(400/pixelsPerMeter,585/pixelsPerMeter);
body = world.CreateBody(bodyDef);
var shape:b2PolygonShape = new b2PolygonShape();
shape.SetAsBox(400/pixelsPerMeter,15/pixelsPerMeter);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = shape;
fixtureDef.friction = 0.3;
fixtureDef.density = 0;
body.CreateFixture(fixtureDef);
var ground:Sprite = new floor() as Sprite;
ground.x = body.GetPosition().x*pixelsPerMeter;
ground.y = body.GetPosition().y*pixelsPerMeter;
addChild(ground);
}
private function makeBodies():void{
for(var i:int=0;i<5;i++){
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.x = (Math.random()*600+120)/pixelsPerMeter;
bodyDef.position.y = (Math.random()*30)/pixelsPerMeter;
var box:b2PolygonShape = new b2PolygonShape();
box.SetAsBox(45/pixelsPerMeter,45/pixelsPerMeter);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = box;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.5;
fixtureDef.restitution = 0.2;
bodyDef.userData = new crate() as Sprite;
body = world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
addChild(bodyDef.userData);
}
}
private function onMouseDown(e:MouseEvent):void{
var body:b2Body = getBodyAtMouse();
if(body){
body.ApplyImpulse(new b2Vec2(0.0,-80.0),body.GetWorldCenter());
body.ApplyTorque(450);
}
}
private function onMouseUp(e:MouseEvent):void{
if(mouseJoint){
world.DestroyJoint(mouseJoint);
mouseJoint = null;
}
}
private function getBodyAtMouse(includeStatic:Boolean = false):b2Body{
var mouseXWorldPhys:Number = mouseX/pixelsPerMeter;
var mouseYWorldPhys:Number = mouseY/pixelsPerMeter;
mousePVec.Set(mouseXWorldPhys,mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(mouseXWorldPhys-0.001,mouseYWorldPhys-0.001);
aabb.upperBound.Set(mouseXWorldPhys+0.001,mouseYWorldPhys+0.001);
var body:b2Body = null;
var fixture:b2Fixture;
function GetBodyCallBack(fixture:b2Fixture):Boolean{
var shape:b2Shape = fixture.GetShape();
if(fixture.GetBody().GetType() != b2Body.b2_staticBody || includeStatic){
var inside:Boolean = shape.TestPoint(fixture.GetBody().GetTransform(),mousePVec);
if(inside){
body = fixture.GetBody();
return false;
}
}
return true;
}
world.QueryAABB(GetBodyCallBack,aabb);
return body;
}
private function onEnterframe(e:Event):void{
world.Step(timeStep,iterations,iterations);
world.ClearForces();
updateTexture();
world.DrawDebugData();
if(mouseJoint){
var xpos:Number = mouseX/pixelsPerMeter;
var ypos:Number = mouseY/pixelsPerMeter;
var v2:b2Vec2 = new b2Vec2(xpos,ypos);
mouseJoint.SetTarget(v2);
}
}
private function updateTexture():void{
for(var body:b2Body = world.GetBodyList();body;body = body.GetNext()){
if(body.GetUserData() is Sprite){
body.GetUserData().x = body.GetPosition().x*pixelsPerMeter;
body.GetUserData().y = body.GetPosition().y*pixelsPerMeter;
body.GetUserData().rotation = body.GetAngle()*180/Math.PI;
}
}
}
}
}
[/code]
转载请注明:陈童的博客 » Box2DFlash的纹理及点选物体