Playing with Box2DFlashAS3
Some time ago I told you to watch Box2DFlashAS3 physics engine.
I dont’ know if you did it, but I did. While the original Box2D project come to 2.0.0 version, the AS3 porting is “still” at 1.4.3, but soon it will be upgraded.
So I think it’s absolutely time to play with this library.
While I won’t be able to provide a full tutorial before some days, I made an interesting prototype in only 58 lines, brackets included.
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 | package {
import flash.display.*;
import flash.events.*;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class theclass extends Sprite {
var body_def:b2BodyDef;
var shape_def:b2BoxDef;
public var the_world:b2World;
public function theclass() {
var container:b2AABB = new b2AABB();
container.minVertex.Set(0.0, 0.0);
container.maxVertex.Set(500.0, 400.0);
the_world = new b2World(container, new b2Vec2(0, 300.0), true);
body_def = new b2BodyDef();
shape_def = new b2BoxDef();
shape_def.extents.Set(250,10);
shape_def.friction = 1;
body_def.position.Set(250,390);
body_def.AddShape(shape_def);
body_def.userData = new floor();
body_def.userData.width = 500;
body_def.userData.height = 20;
addChild(body_def.userData);
the_world.CreateBody(body_def);
addEventListener(Event.ENTER_FRAME, render);
addEventListener(MouseEvent.CLICK, mouse_handler);
}
private function mouse_handler(event:MouseEvent):void {
body_def = new b2BodyDef();
shape_def = new b2BoxDef();
shape_def.extents.Set(Math.floor(Math.random()*15)+15,Math.floor(Math.random()*15)+15);
shape_def.friction = 1;
shape_def.density = 1;
shape_def.restitution = 0.2;
body_def.position.Set(Math.random()*400+50,0);
body_def.AddShape(shape_def);
body_def.userData = new crate();
body_def.userData.width = shape_def.extents.x * 2;
body_def.userData.height = shape_def.extents.y * 2;
addChild(body_def.userData);
body_def.userData.gotoAndStop(Math.floor(Math.random()*6)+1);
the_world.CreateBody(body_def);
}
public function render(e:Event):void {
the_world.Step(1/30, 10);
for (var x:b2Body = the_world.m_bodyList; x; x = x.m_next) {
if (x.m_userData is Sprite) {
x.m_userData.x = x.m_position.x;
x.m_userData.y = x.m_position.y;
x.m_userData.rotation = x.m_rotation * 57.2957795;
}
}
}
}
} |
And the result is really interesting, and virtually impossible to achieve without the library… just click on the floor and see what happens…
I think it’s time to start learning AS3 with this interesting library… meanwhile you can download the source code with the last version of Box2DFlashAS3 library.
They can be easily customized to meet the unique requirements of your project.
26 Responses to “Playing with Box2DFlashAS3”
Leave a Reply
Trackbacks
-
Create a Flash ball game using AS3 : Emanuele Feronato - italian geek and PROgrammer on
March 25th, 2008 9:38 am
[...] Actionscript 3. Even if AS2 is way to be dead, is becoming a bit obsolete… every new library like Box2DFlashAS3 only works in AS3, so sooner or later we must learn [...]
-
Create a Flash game like Totem Destroyer : Emanuele Feronato - italian geek and PROgrammer on
July 18th, 2008 9:33 pm
[...] already talked about it in the post Playing with Box2DFlashAS3, now it’s time to make something more [...]
-
Adobe Flash CS3- AS3 and AS2 game tutorials roundup | Lemlinh.com on
August 19th, 2008 6:30 pm
[...] already talked about it in the post Playing with Box2DFlashAS3, now it’s time to make something more [...]
-
Platform game basics using Box2D : Emanuele Feronato on
October 26th, 2008 7:42 pm
[...] wrote about Box2D Flash version about a year ago, then I published Playing with Box2DFlashAS3 and Create a Flash game like Totem Destroyer as examples covering what you can do 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)





Wow… Very cool… =D
Hey emanuele, you just earned 20 bucks from me because i just got 100 dollars… i think u were my referrer. i hope…
Nice…Would you know how to change it to AS2?
It’s not possible AFAIK
I guess we all do the same thing as the first test of the engine :)
how do I open that .fla? it says “unexpected file format.” :O
You gotta use flash CS3, Orava. That message means you’re using older version.
Nice. This will help me prevent collisions in my game.
Box2D is awesome! I just finished making my first Flash game with it, which you can find if you click my name. While theoretically you could port the engine to AS2, it would be pointless because it would run like 10% as fast. Just embrace AS3 and enjoy the speed!
I think it was ported to AS2, but alongside every other AS2 physics engine, it has little to no documentation with a steep ass learning curve for newcomers to using libraries.
Ie, me.
I believe you are incorrect about it being ported to AS2, if you can show me a link I will happily admit that I’m wrong :) No documentation? You’re kidding right?
http://box2d.org/manual.html
Most people are just too lazy to read it. It IS slightly more complicated than APE for instance, but it has many more features and is faster!
What a great idea! Putting advertisements in your source code! I wish more people would nest ads in their “open source” source. After all, it’s about the money…isn’t it?
lol it’s not my fault, it’s just that kontera put a link in the source.
How do I put Box2D into my classpath? I don’t know where to put it.
AS3 is awesome. this is so amazing
one word to explain this”AWESOME”
would love to learn hw to use this engine
Damn, it’s totally cool i love it
You wouldn’t be able to work out how to use flade would you?
That’s
http://www.cove.org/flade/
I love physics but I don’t have flash CS3.
I need a beginners tutorial for action script 3. I have no idea how to work box 2D with flash CS3. And I don’t know action script at all so basically i need to know how classes are inputted into a flash file.
There’s a flash-based editor for Box2D, that generates the code. Might be useful if you use Box2d AS3.
http://www.brainblitz.org/BoxCAD
how the hell do i get AS3 onto my CS3 flash program!?!?!
I really need some help!!!!