Talking about Actionscript 3, Box2D, Flash and Users contributions.
Some time ago I published a Basic Box2D editor using Flash movieclips.
Now it’s time to show you something way more interesting called Bison Kick by Jacob Schatz who runs the blog jacobschatz.com (bookmark it! It contains a lot of useful information).
It’s an online Box2D editor with live preview.
Besides it’s still under development, with more options to be added and a few bugs to be removed, it’s very fun and simple to use.
I tried to design a level to be used in a game like Totem Destroyer and I managed to export it both in AS3:
//order: x , y , height, width, rotation, isDynamic, shape, friction,density,restitution,ID
var map:Array = [
[175,324,50,50,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[325,325,50,50,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[250,375,50,500,0,false,'SQUARE' , 1,0.5,0.5,'ground']
,[250,275,50,300,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[250,200,100,150,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[125,225,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[250,125,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[375,225,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[250,50,100,50,0,true,'SQUARE' , 1,0.25,0.5,'totem']
];
and in XML:
175
324
50
50
0
true
SQUARE
1,0.5,0.5,'normal'
325
325
50
50
0
true
SQUARE
1,0.5,0.5,'normal'
250
375
50
500
0
false
SQUARE
1,0.5,0.5,'ground'
250
275
50
300
0
true
SQUARE
1,0.5,0.5,'normal'
250
200
100
150
0
true
SQUARE
1,0.5,0.5,'normal'
125
225
50
50
0
true
SQUARE
1,2,0.5,'heavy'
250
125
50
50
0
true
SQUARE
1,2,0.5,'heavy'
375
225
50
50
0
true
SQUARE
1,2,0.5,'heavy'
250
50
100
50
0
true
SQUARE
1,0.25,0.5,'totem'
Then, creating a Box2D world from these data was really easy thanks to the snippet of code provided by Jacob:
package
{
import Box2D.Dynamics.Joints.*;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.Event;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class Main extends Sprite {
public var world:b2World=new b2World(new b2Vec2(0,10.0),true);
public var world_scale:int = 30;
public static const SQUARE:String = "SQUARE";
public static const CIRCLE:String = "CIRCLE";
public var fixtureDef:b2FixtureDef = new b2FixtureDef();
public var specialB2Body:b2Body;
public var motorOn:Boolean;
//order: x , y , height, width, rotation, isDynamic, shape, friction,density,restitution,ID
var map:Array = [
[175,324,50,50,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[325,325,50,50,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[250,375,50,500,0,false,'SQUARE' , 1,0.5,0.5,'ground']
,[250,275,50,300,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[250,200,100,150,0,true,'SQUARE' , 1,0.5,0.5,'normal']
,[125,225,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[250,125,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[375,225,50,50,0,true,'SQUARE' , 1,2,0.5,'heavy']
,[250,50,100,50,0,true,'SQUARE' , 1,0.25,0.5,'totem']
]; //
public function Main():void
{
debug_draw();
for (var i:uint = 0; i < map.length; i++)
{
var sx:Number = map[i][0];
var sy:Number = map[i][1];
var sizerh:Number = map[i][2];
var sizerw:Number = map[i][3];
var srot:Number = map[i][4];
var sIsDynamic:* = map[i][5];
var sShape:String = map[i][6];
var sFric:Number = map[i][7];
var sMass:Number = map[i][8];
var sRest:Number = map[i][9];
var boxId:int = map[i][10];
if (sShape == SQUARE)
{
draw_box(sx, sy, sizerw, sizerh, sIsDynamic,srot,sFric,sMass,sRest,boxId);
}
else if (sShape == CIRCLE)
{
draw_circle(sx, sy, sizerw / 2, sIsDynamic,srot,sFric,sMass,sRest,boxId);
}
}
addEventListener(Event.ENTER_FRAME, update);
}
public function draw_circle(px:Number, py:Number, r:Number, d:Boolean, rot:Number,
fric:Number,mass:Number,rest:Number,boxId:int):void {
var my_body:b2BodyDef= new b2BodyDef();
my_body.position.Set(px / world_scale, py / world_scale);
if (d)
{
my_body.type=b2Body.b2_dynamicBody;
}
my_body.angle = rot;
var my_circle:b2CircleShape=new b2CircleShape(r/world_scale);
var my_fixture:b2FixtureDef = new b2FixtureDef();
my_fixture.density = mass;
my_fixture.friction = fric;
my_fixture.restitution = rest;
my_fixture.shape=my_circle;
var world_body:b2Body = world.CreateBody(my_body);
world_body.CreateFixture(my_fixture);
}
public function draw_box(px:Number, py:Number, w:Number, h:Number, d:Boolean, rot:Number,
fric:Number,mass:Number,rest:Number,boxId:int):void {
var my_body:b2BodyDef = new b2BodyDef();
my_body.position.Set(px / world_scale, py / world_scale);
my_body.angle = rot;
if (d)
{
my_body.type=b2Body.b2_dynamicBody;
}
var my_box:b2PolygonShape = new b2PolygonShape();
my_box.SetAsBox(w/2/world_scale, h/2/world_scale);
var my_fixture:b2FixtureDef = new b2FixtureDef();
my_fixture.density = mass;
my_fixture.friction = fric;
my_fixture.restitution = rest;
my_fixture.shape=my_box;
var world_body:b2Body=world.CreateBody(my_body);
world_body.CreateFixture(my_fixture);
}
public function debug_draw():void {
var debug_draw:b2DebugDraw = new b2DebugDraw();
var debug_sprite:Sprite = new Sprite();
addChild(debug_sprite);
debug_draw.SetSprite(debug_sprite);
debug_draw.SetFillAlpha(0.3);
debug_draw.SetDrawScale(world_scale);
debug_draw.SetFlags(b2DebugDraw.e_shapeBit);
world.SetDebugDraw(debug_draw);
}
public function update(e : Event):void {
world.Step(1 / 30, 10, 10);
world.ClearForces();
world.DrawDebugData();
}
}
}
... and this is what you'll get:
This the same scene of the screenshot you can see at the beginning of this post.
Next time, I'll show you how to generate the same thing reading an external XML, and how to add more features.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.