Talking about Actionscript 3, Box2D, Flash and Game development.
When I made the PhysInjector game prototype in only 100 lines of code I told you you would need a lot more lines (and work) in order to make the same thing run with pure Box2D.
Today I am showing you how much work.. this is the main class:
package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.Event; import flash.text.TextField; import Box2D.Dynamics.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import Box2D.Dynamics.Contacts.*; import Box2D.Dynamics.Joints.*; public class Main extends Sprite { private var world:b2World=new b2World(new b2Vec2(0,10),true); private var worldScale:Number=30; private var joint:b2RevoluteJoint; private var bodyFloor:b2Body; private var floor:Sprite= new Sprite(); private var colorArray:Array=[0xff0000,0x00ff00,0x0000ff,0xffff00,0xff00ff,0x00ffff,0x000000,0xffffff]; private var boxColor:Number=Math.floor(Math.random()*colorArray.length); private var lives:Number=3; private var score:Number=0; private var scoreText:TextField = new TextField(); public function Main() { var customContact:CustomContactListener=new CustomContactListener(); world.SetContactListener(customContact); graphics.lineStyle(2,0x000000); graphics.moveTo(0,100); graphics.lineTo(640,100); addChild(scoreText); floor.x=50; floor.y=420; addChild(floor); fillFloor(); var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.position.Set(320/worldScale,430/worldScale); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.userData={name:"floor",asset:floor}; var polygonShape:b2PolygonShape=new b2PolygonShape(); polygonShape.SetAsBox(270/worldScale,10/worldScale); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=polygonShape; fixtureDef.density=10; fixtureDef.restitution=0; fixtureDef.friction=0.2; bodyFloor=world.CreateBody(bodyDef); bodyFloor.CreateFixture(fixtureDef); var revoluteJointDef:b2RevoluteJointDef=new b2RevoluteJointDef(); revoluteJointDef.localAnchorA.Set(0,0); revoluteJointDef.localAnchorB.Set(320/worldScale,430/worldScale); revoluteJointDef.bodyA=bodyFloor; revoluteJointDef.bodyB=world.GetGroundBody(); revoluteJointDef.enableMotor=true; joint=world.CreateJoint(revoluteJointDef) as b2RevoluteJoint; var sensor:Sprite=new Sprite(); sensor.graphics.beginFill(0x000000); sensor.graphics.drawRect(-640,-10,1280,20); sensor.graphics.endFill(); addChild(sensor); sensor.x=-320; sensor.y=500; bodyDef.position.Set(320/worldScale,510/worldScale); bodyDef.type=b2Body.b2_staticBody; bodyDef.userData={name:"sensor",asset:sensor}; polygonShape.SetAsBox(640/worldScale,10/worldScale); fixtureDef.shape=polygonShape; fixtureDef.isSensor=true; var sensorBody:b2Body=world.CreateBody(bodyDef); sensorBody.CreateFixture(fixtureDef); addEventListener(Event.ENTER_FRAME,update); stage.addEventListener(MouseEvent.CLICK,addBox); } private function fillFloor():void { floor.graphics.clear(); floor.graphics.beginFill(colorArray[boxColor]); floor.graphics.drawRect(-270,-10,540,20); floor.graphics.endFill(); } private function addBox(e:MouseEvent):void { if (mouseY<100) { var box:Sprite = new Sprite(); box.graphics.beginFill(colorArray[boxColor]); box.graphics.drawRect(-25,-25,50,50); box.graphics.endFill(); box.x=mouseX-25; box.y=mouseY-25; addChild(box); var bodyDef:b2BodyDef=new b2BodyDef(); bodyDef.position.Set(mouseX/worldScale,mouseY/worldScale); bodyDef.type=b2Body.b2_dynamicBody; bodyDef.userData={name:"box",asset:box,val:boxColor,hit:false,destroy:false}; var polygonShape:b2PolygonShape=new b2PolygonShape(); polygonShape.SetAsBox(25/worldScale,25/worldScale); var fixtureDef:b2FixtureDef=new b2FixtureDef(); fixtureDef.shape=polygonShape; fixtureDef.density=1+score/100; fixtureDef.restitution=0; fixtureDef.friction=0.2; var body:b2Body=world.CreateBody(bodyDef); body.CreateFixture(fixtureDef); boxColor=Math.floor(Math.random()*colorArray.length); fillFloor(); } } private function update(event:Event):void { world.Step(1/30,10,10); for (var currentBody:b2Body=world.GetBodyList(); currentBody; currentBody=currentBody.GetNext()) { if (currentBody.GetUserData()) { currentBody.GetUserData().asset.x=currentBody.GetPosition().x*worldScale; currentBody.GetUserData().asset.y=currentBody.GetPosition().y*worldScale; currentBody.GetUserData().asset.rotation=currentBody.GetAngle()*(180/Math.PI); if (currentBody.GetUserData().hit) { currentBody.GetUserData().hit=false; score+=0.5; currentBody.GetUserData().asset.alpha-=0.25; if (currentBody.GetUserData().asset.alpha<=0) { score+=2.5; removeChild(currentBody.GetUserData().asset); world.DestroyBody(currentBody); } scoreText.text=score.toString()+" - "+lives.toString()+" lives"; } if (currentBody.GetUserData().destroy) { removeChild(currentBody.GetUserData().asset); world.DestroyBody(currentBody); lives--; scoreText.text=score.toString()+" - "+lives.toString()+" lives"; if (lives==0) { removeEventListener(Event.ENTER_FRAME,update); stage.removeEventListener(MouseEvent.CLICK,addBox); } } } } joint.SetMotorSpeed(bodyFloor.GetUserData().asset.rotation); joint.SetMaxMotorTorque(Math.abs(bodyFloor.GetUserData().asset.rotation)*100); world.ClearForces(); } } }
… and this is the custom contact listener
package { import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Dynamics.Joints.*; import Box2D.Dynamics.Contacts.*; import Box2D.Common.Math.b2Vec2; class CustomContactListener extends b2ContactListener { override public function BeginContact(contact:b2Contact):void { var bodyA:b2Body=contact.GetFixtureA().GetBody(); var bodyB:b2Body=contact.GetFixtureB().GetBody(); if (bodyA.GetUserData().name=="box"&&bodyB.GetUserData().name=="box"&&bodyA.GetUserData().val==bodyB.GetUserData().val) { bodyA.GetUserData().hit=true; bodyB.GetUserData().hit=true; } if (bodyA.GetUserData().name=="sensor"&&bodyB.GetUserData().name=="box") { bodyB.GetUserData().destroy=true; } if (bodyA.GetUserData().name=="box"&&bodyB.GetUserData().name=="sensor") { bodyA.GetUserData().destroy=true; } } } }
As you can see, the whole script is 138+23 = 161 lines long, moreover I had to override a function, create one more class, and spend more time.
That’s why I am using PhysInjector library for a quick game I am going to release next week.
Anyway, this is the result of the Box2D version:
Click with mouse button above the black line and match boxes of the same color.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.