Platform engine using Box2D
Filed Under Actionscript 3, Box2D, Flash, Game design • 7 Comments
Platform engines always represented a challenge for me, you can see my prototypes with AS2 and AS3, and this time I am going to make one using Box2D.
Box2D is used as physics engine by PlayCrafter guys, and since the results you can see in Rick Triqui game are really interesting, I’ll start coding my little personal engine to make platform games.
The following script is an adaptation of Platform game basics using Box2D but I am going to add it a lot of options in order to let you use it for your projects.
I am also using the input class included in Box2D, why writing my own one if that one works?
At the moment it’s only a bunch of lines starting from the Hello World example, but starting from tomorrow I’ll code the main character shooting as seen in Rick Triqui.
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 | package { import flash.display.Sprite; import flash.events.Event; import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; import General.Input; import flash.events.MouseEvent; public class HelloWorld extends Sprite { public var m_world:b2World; public var pixels_in_a_meter:int=30; public var bodyDef:b2BodyDef; public var boxDef:b2PolygonDef; public var m_input:Input; public var xspeed:int=0; var body:b2Body; public function HelloWorld() { addEventListener(Event.ENTER_FRAME, Update, false, 0, true); var worldAABB:b2AABB = new b2AABB(); worldAABB.lowerBound.Set(-100.0, -100.0); worldAABB.upperBound.Set(100.0, 100.0); var gravity:b2Vec2=new b2Vec2(0.0,10.0); var doSleep:Boolean=true; m_world=new b2World(worldAABB,gravity,doSleep); var m_sprite:Sprite; m_sprite = new Sprite(); addChild(m_sprite); 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); var bodyDef:b2BodyDef; var boxDef:b2PolygonDef; var circleDef:b2CircleDef; square_tile(250,395,500,10); hero(100,370,10,40); // Box2D input class - why should I bother making my one? m_sprite = new Sprite(); addChild(m_sprite); // input m_input=new Input(m_sprite); } public function square_tile(px:int,py:int,w:int,h:int) { bodyDef = new b2BodyDef(); bodyDef.position.Set(px/pixels_in_a_meter, py/pixels_in_a_meter); boxDef = new b2PolygonDef(); boxDef.SetAsBox(real_pixels(w), real_pixels(h)); boxDef.friction=0.3; boxDef.density=0; body=m_world.CreateBody(bodyDef); body.CreateShape(boxDef); body.SetMassFromShapes(); } public function hero(px:int, py:int, w:int, h:int) { bodyDef = new b2BodyDef(); bodyDef.position.Set(px/pixels_in_a_meter, py/pixels_in_a_meter); boxDef = new b2PolygonDef(); boxDef.SetAsBox(real_pixels(w), real_pixels(h)); boxDef.density=1.0; boxDef.friction=0.3; boxDef.restitution=0.2; bodyDef.userData = new Sprite(); bodyDef.userData.name="Player"; body=m_world.CreateBody(bodyDef); body.SetBullet(true); body.CreateShape(boxDef); body.SetMassFromShapes(); } public function real_pixels(n:int) { return (n/pixels_in_a_meter/2); } public function Update(e:Event):void { m_world.Step(1/30, 10); for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) { if (bb.GetUserData()!=null) { if (Input.isKeyPressed(39)) { xspeed=3; } else if (Input.isKeyPressed(37)) { xspeed=-3; } if (xspeed) { bb.WakeUp(); bb.m_linearVelocity.x=xspeed; } bb.m_sweep.a = 0; } } Input.update(); } } } |
Here it is the result… no need to download, just copy/paste these lines in the Hello World example.
Next time I’ll add graphics and the bazooka, and obviously I’ll explain the script, meanwhile… left/right arrows to move the player.
They can be easily customized to meet the unique requirements of your project.
7 Responses to “Platform engine using Box2D”
Leave a Reply
Trackbacks
-
Platform engine using Box2D – Step 2 : Emanuele Feronato on
July 14th, 2009 3:52 pm
[...] publishing Platform engine using Box2D, my attempt to replicate the Rick Triqui’s main character [...]
-
Platform engine using Box2D – Step 3 : Emanuele Feronato on
July 31st, 2009 12:56 pm
[...] I suggest you to read parts 1 and 2 if you already didn’t, then you should know something about the magic of compound [...]
-
Platform engine using Box2D – Step 3 by Blam Yo! on
August 5th, 2009 1:44 am
[...] I suggest you to read parts 1 and 2 if you already didn’t, then you should know something about the magic of compound [...]
- Citrus Engine released for free for learning
- My epic fail with ClickBank
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 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
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a survival horror game in Flash tutorial – part 1 (4.74/5)
- Create a flash artillery game – step 2 (4.74/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 1 (4.71/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)


(20 votes, average: 3.75 out of 5)



Perhaps you could use multiple classes and return types?
I like the way you circumvented the body definition’s fixedRotation and let the player tilt a little bit.
Is this a cure against slopes? Can’t think of a benefit for graphical representation though.
I have found Push Button Engine which combines Box2D with other components to make games faster. Maybe you can use it to do something.
i have attached a movieclip to the userdata of my body(b2body)…say myBody.userData = new Xobj(); i have different frame labels inside the movieclip(Xobj) holding different poses.if i place gotoAndStop(“xLabel”); my character is not jumping properly, if i comment the gotoAndStop line, my character is jumping and moving well. So how move between different labels? and what might be the cause for this weird behaviour?