Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Actionscript 3, Box2D and Flash.

Do you want to add physics to your Flash projects with almost no code, keeping your scripts simple with only a few modifications, injecting physics attributes directly on your Display Ojects?

Then PhysInjector is the library you’ll love.

PhysInjector is a collection of powerfull factory and wrapper classes developed to work specifically with Box2D and does not do any collision detection on its own, rather it significantly simplifies the use of Box2D within your Flash games and applications while providing an array of handy plugins and helper classes.

PhysInjector takes a totally different approach than other conventional physics libraries. Instead of you having to create a physics object and apply a display object as a skin, it actually uses display objects that have already been added to the stage and “injects” them with physics properties. You don’t have to worry about the display object’s position, scale or rotation! PhysInjector will handle that for you!

I made a little example to let you see how easy is to ijnect physics into Display Objects.

The following script just adds black squares when you click the mouse, with no physics at all, it just places black squares on the screen.

Something like “my first AS3 script”:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	public class Main extends Sprite {
		public function Main() {
			var floor:Sprite = new Sprite();
			floor.graphics.beginFill(0x000000);
			floor.graphics.drawRect(0,0,640,20);
			floor.graphics.endFill();
			floor.x=0;
			floor.y=460;
			addChild(floor);
			stage.addEventListener(MouseEvent.CLICK,addBox);
		}
		private function addBox(e:MouseEvent):void{
			var box:Sprite = new Sprite();
			box.graphics.beginFill(0x000000);
			box.graphics.drawRect(0,0,50,50);
			box.graphics.endFill();
			box.x=mouseX-25;
			box.y=mouseY-25;
			addChild(box);	
		}		
	}
}

And this is the result:

Click on the stage to add a black square.

Now, look at the same example, with physics injection: it’s very easy to understand:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import Box2D.Common.Math.b2Vec2;
	import com.reyco1.physinjector.PhysInjector;
	import com.reyco1.physinjector.data.PhysicsObject;
	import com.reyco1.physinjector.data.PhysicsProperties;
	public class Main extends Sprite {
		private var physics:PhysInjector;
		public function Main() {
			physics=new PhysInjector(stage,new b2Vec2(0,10),true);
			var floor:Sprite = new Sprite();
			floor.graphics.beginFill(0x000000);
			floor.graphics.drawRect(0,0,640,20);
			floor.graphics.endFill();
			floor.x=0;
			floor.y=460;
			addChild(floor);
			var box2dFloor:PhysicsObject=physics.injectPhysics(floor,PhysInjector.SQUARE,new PhysicsProperties({isDynamic:false,friction:0.5,restitution:0.5}));
			addEventListener(Event.ENTER_FRAME,update);
			stage.addEventListener(MouseEvent.CLICK,addBox);
		}
		private function addBox(e:MouseEvent):void {
			var box:Sprite = new Sprite();
			box.graphics.beginFill(0x000000);
			box.graphics.drawRect(0,0,50,50);
			box.graphics.endFill();
			box.x=mouseX-25;
			box.y=mouseY-25;
			addChild(box);
			var box2dBox:PhysicsObject=physics.injectPhysics(box,PhysInjector.SQUARE,new PhysicsProperties({isDynamic:true,friction:0.2,restitution:0.5}));
		}
		private function update(event:Event):void {
			physics.update();
		}
	}
}

And see what happens:

Click on the stage to add black squares which will turn into dynamic bodies.

Did you see how quick and easy it is? Download the source code with all needed libraries.

Next time I’ll show you how to code a complete physics game prototype just using a couple of lines (well, maybe a bit more, but you got the point).

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.