Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Hundreds game, Actionscript 3, Flash and Game development.

Today, something interesting for Nape users… I made the “Hundreds” Flash game prototype using Box2D with Nape, so you can decide what’s better.

It works the same way move the mouse over a circle and see…

… but the source code is different:

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import nape.geom.Vec2;
	import nape.phys.Body;
	import nape.phys.BodyList;
	import nape.phys.BodyType;
	import nape.shape.Polygon;
	import nape.shape.Circle;
	import nape.space.Space;
	import nape.util.ShapeDebug;
	import nape.shape.ShapeList;

	public class Main extends Sprite {
		private var napeWorld:Space=new Space(new Vec2(0,0));
		private var debug:ShapeDebug=new ShapeDebug(640,480,0x00ff00);
		private var napeDebugSprite:Sprite=new Sprite();
		private var circleSpeed:Number=150;
		public function Main():void {
			addChild(napeDebugSprite);
			napeDebugSprite.addChild(debug.display);
			for (var i:Number=1; i<=10; i++) {
				addCircle();
			}
			addWall(320,0,640,10);
			addWall(320,480,640,10);
			addWall(0,240,10,480);
			addWall(640,240,10,480);
			addEventListener(Event.ENTER_FRAME,update);
		}
		private function addCircle():void {
			var napeBody:Body=new Body(BodyType.DYNAMIC,new Vec2(Math.round(Math.random()*600+20),Math.round(Math.random()*440+20)));
			var circle:Circle=new Circle(10);
			circle.material.elasticity=0.5;
			circle.material.density=1;
			circle.material.staticFriction=0;
			napeBody.shapes.add(circle);
			napeBody.space=napeWorld;
			napeBody.userData.name="circle";
			var randomAngle:Number=Math.random()*2*Math.PI;
			napeBody.velocity=new Vec2(circleSpeed*Math.cos(randomAngle),circleSpeed*Math.sin(randomAngle));
		}
		private function addWall(pX:Number,pY:Number,w:Number,h:Number):void {
			var napeBody:Body=new Body(BodyType.STATIC,new Vec2(pX,pY));
			var polygon:Polygon=new Polygon(Polygon.box(w,h));
			polygon.material.elasticity=0.5;
			polygon.material.density=1;
			polygon.material.staticFriction=0;
			napeBody.shapes.add(polygon);
			napeBody.space=napeWorld;
		}
		private function update(e:Event):void {
			napeWorld.step(1/30,10,10);
			var bodies:BodyList=napeWorld.bodies;
			for (var i:int = 0; i < bodies.length; i++) {
				var body:Body=bodies.at(i);
				if (body.userData.name=="circle") {
					var velocity:Vec2=body.velocity;
					var speed:Number=velocity.length;
					var ratio:Number=circleSpeed/speed;
					body.velocity.muleq(ratio);
				}
			}
			bodies=napeWorld.bodiesUnderPoint(new Vec2(mouseX,mouseY));
			if (bodies.length>0) {
				for (i = 0; i < bodies.length; i++) {
					body=bodies.at(i);
					if (body.userData.name=="circle") {
						var shapes:ShapeList=body.shapes;
						var circle:Circle=shapes.at(i) as Circle;
						if(circle.radius<50){
							circle.radius++
							}
					}
				}
			}
			debug.clear();
			debug.draw(napeWorld);
			debug.flush();
		}
	}
}

Next time, something you won't find easily around the web, Nape collision handling

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