Creation of a Flash Stabilize! clone using Box2D – part 1
Filed Under Actionscript 3, Box2D, Flash, Game design • 8 Comments
In Triqui’s picks 1 I told you Stabilize! is a good game to clone using Box2D.
And I would use this post to focus on another thing too… it’s about recycling.
I mean… once you code for a while, or follow developer’s blogs like this one providing a lot of source codes, probably most of the stuff you need has already been written here and there.
In this case, the script we are going to modify is Drawing boxes on the fly in Box2D.
You will see we can make the whole game simply changing the original script step by step.
Coding this way will speed up your learning curve because you are starting from a working project so you don’t have to worry about the overall environment, and you can focus on small changes.
In this first step, we’ll just spend a couple of words about the balance bar.
It’s made up with a static object (the fulcrum) and a dynamic one (the bar).
In order to work, the density of the bar should be quite greater than the one of the falling boxes, or you’ll risk to lose the bar once the first box falls on it.
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; public class stabilize extends Sprite { public var m_sprite: Sprite = new Sprite(); public var m_world:b2World; public var initX:Number=0.0; public var initY:Number=0.0; public var drawing:Boolean=false; public function stabilize() { var gravity:b2Vec2=new b2Vec2(0,9.8); var worldAABB:b2AABB = new b2AABB(); worldAABB.lowerBound.Set(-1000,-1000); worldAABB.upperBound.Set(1000,1000); m_world=new b2World(worldAABB,gravity,true); m_sprite = new Sprite(); addChild(m_sprite); debug_draw(); AddBox(250/30,350/30,10/30,10/30,0); AddBox(250/30,350/30,200/30,10/30,20); addEventListener(Event.ENTER_FRAME,Update); stage.addEventListener(MouseEvent.MOUSE_DOWN,mousePressed); stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoved); stage.addEventListener(MouseEvent.MOUSE_UP,mouseReleased); } public function mousePressed(e:MouseEvent) { //Store initial X and Y position initX=e.localX; initY=e.localY; drawing=true; } public function mouseMoved(e:MouseEvent) { if (drawing) { //Simply draw the "ghost" of the box we are about to add graphics.clear(); graphics.beginFill(0xFF0000,0.5); graphics.drawRect(initX,initY,e.localX-initX,e.localY-initY); } } public function mouseReleased(e:MouseEvent) { graphics.clear(); drawing=false; //Coordinates of bottom-right of box (when drawing from left to right) var finalX:Number=e.localX; var finalY:Number=e.localY; //Correct if drawing from right to left if (finalX<initX) { //If so, swap initX and finalX var tempX:Number=initX; initX=finalX; finalX=tempX; } if (finalY<initY) { //If so, swap initY and finalY var tempY:Number=initY; initY=finalY; finalY=tempY; } //Work out the half-width and height of the box var boxHalfWidth:Number = Math.abs((finalX-initX)/2); var boxHalfHeight:Number = Math.abs((finalY-initY)/2); if (boxHalfWidth>0&&boxHalfHeight>0) { AddBox((finalX-boxHalfWidth)/30,(finalY-boxHalfHeight)/30,boxHalfWidth/30,boxHalfHeight/30,1); } } /* NOTE: AddBox takes the _x,_y and halfwidth and halfheight parameters in METRES not PIXELS. This means when you call this function, always DIVIDE a pixel size by 30 to get it in meteres. */ public function AddBox(_x:Number,_y:Number,_halfwidth:Number,_halfheight:Number,density:Number) { var bodyDef:b2BodyDef = new b2BodyDef(); bodyDef.position.Set(_x,_y); var boxDef:b2PolygonDef = new b2PolygonDef(); boxDef.SetAsBox(_halfwidth,_halfheight); boxDef.density=density; boxDef.friction=0.3; boxDef.restitution=0.2; var body:b2Body=m_world.CreateBody(bodyDef); body.CreateShape(boxDef); body.SetMassFromShapes(); } public function Update(e:Event) { //We need to do this to simulate physics m_world.Step(1/30,10); } public function debug_draw() { var dbgDraw:b2DebugDraw = new b2DebugDraw(); var dbgSprite:Sprite = new Sprite(); m_sprite.addChild(dbgSprite); dbgDraw.m_sprite=m_sprite; dbgDraw.m_drawScale=30; dbgDraw.m_alpha=1; dbgDraw.m_fillAlpha=0.5; dbgDraw.m_lineThickness=1; dbgDraw.m_drawFlags=b2DebugDraw.e_shapeBit; m_world.SetDebugDraw(dbgDraw); } } } |
And this is the result:
Draw boxes with your mouse and see how does the bar react.
No need to download anything, simply cut/paste the script you can find at Drawing boxes on the fly in Box2D.
Next time, we’ll manage “true” crates.
They can be easily customized to meet the unique requirements of your project.
8 Responses to “Creation of a Flash Stabilize! clone using Box2D – part 1”
Leave a Reply
Trackbacks
-
Creation of a Flash Stabilize! clone using Box2D – part 2 : Emanuele Feronato on
October 29th, 2009 11:14 pm
[...] Time to add some new features to our Stabilize! clone we started in this post. [...]
-
Creation of a Flash Stabilize! clone using Box2D – part 3 : Emanuele Feronato on
November 10th, 2009 4:25 pm
[...] steps 1 and 2, then continue with this [...]
- Create incredible particle effects with Partigen 2
- PixelBlitz AS3 game framework
- Being a geek in Venezuela
- Box2D tutorial for the absolute beginners – revamped
- Triqui’s Picks #16
- Are you ready for this?
- Box2DFlash 2.1a released – what changed
- Get detailed statistics about your Flash game with SWFStats
- Games that Challenge the World Come2Play contest – $8,000 in prizes
- Triqui’s Picks #15
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Create a flash artillery game - step 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Flash game creation tutorial – part 5.2 (4.87/5)
- Create a flash artillery game – step 1 (4.78/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Flash game creation tutorial – part 3 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.70/5)
- Create a flash artillery game – step 2 (4.70/5)
- Flash game creation tutorial – part 1 (4.69/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)

(7 votes, average: 4.43 out of 5)



Thank you very much! Im going to implement this idea really soon ;)
Thanks! I’m waiting the part 2.
btw,You never finished the Rope and Hook tutorial :-(
Very thin forms will go through without being stopped by anything.
Hi ,
in the truck game demo you have the add wheel function and you set there wheel1.
the question is how do I reset the wheel1.angle?
If I use wheel1.GetAngle I can only see it but can’t set it and if I try with wheel1.angle=0; or wheel1.rotation=0; doens’t work.. please help
thanks!
you’re awesome!
Yake at look at this Game Engine ..
http://pushbuttonengine.com/
Ivan H.