“Hundreds” Flash game prototype – Nape version
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
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
They can be easily customized to meet the unique requirements of your project.





(5 votes, average: 4.80 out of 5)






This post has 3 comments
rreis
Hi Emanuele, how are you?
This is a great tutorial. I´ve just started studying Nape and really liked it.
Could I give you a suggestion for the next tutorial using Nape?
A prototype of a Pool Game. I really like Pool Games and I know other programmers would like to study it too.
Once more, thank you for sharing with us your knowledge.
Regards,
The Don
As a starter with Nape and Starling, this little Tut is top notch and much appreciated. Keep it going Emanuele.
Jack
Great tutorial.
I thinks code “var circle:Circle=shapes.at(i) as Circle;” should be
“var circle:Circle=shapes.at(0) as Circle;”