Talking about Way of an Idea game, Actionscript 3, Box2D, Flash and Game development.
It’s time to apply the concepts seen at Pausing a Box2D simulation with Way of an Idea Box2D prototype to create a paused Box2D simulation that will start only when you’ll press SPACE, and meanwhile you can draw paths, just like in the original game.
The only problem with this technique is you can’t use the debug draw to test your projects, because debug draw draws only after Step
function has been executed, and since the game starts with the simulation paused, you can’t see the Box2D boxes you are drawing (and you can’t even see other assets too) until you start the simulation.
So the trick is: placing the Box2D boxes, the ones debug draw won’t draw, and attaching the movieclips to them. The movieclips will be rendered because I am adding them to stage, and they will remain in position until the simulation starts.
So the script becomes:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
public class draw extends Sprite {
public var drawing:Boolean=false;
public var canvas:Sprite = new Sprite();
public var points_array:Array;
public var pixel_dist:int=20;
public var saved_x:int;
public var saved_y:int;
public var m_world:b2World;
public var body:b2Body;
public var bodyDef:b2BodyDef = new b2BodyDef();
public var circleDef:b2CircleDef= new b2CircleDef();
// radians to degrees conversion
public var rad_to_deg:Number=57.2957795;
// simulation timestep. We start at zero.
public var timestep:Number=0;
public function draw():void {
// world creation
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);
m_world=new b2World(worldAABB,new b2Vec2(0,10),true);
//
// *** IMPORTANT: NOTICE THERE ISN'T ANY DEBUG DRAW ROUTINE ***
//
// placing the ball
bodyDef.position.Set(20/30,20/30);
circleDef.radius=15/30;
circleDef.density=1;
circleDef.friction=0.5;
circleDef.restitution=0.2;
// ball's movieclip attachment
bodyDef.userData = new sphere();
bodyDef.userData.x=20;
bodyDef.userData.y=20;
bodyDef.userData.width=30;
bodyDef.userData.height=30;
addChild(bodyDef.userData);
// end ball's movieclip attachment
body=m_world.CreateBody(bodyDef);
body.CreateShape(circleDef);
body.SetMassFromShapes();
//
addChild(canvas);
canvas.graphics.lineStyle(5);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouse_pressed);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mouse_moved);
stage.addEventListener(MouseEvent.MOUSE_UP,mouse_released);
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
}
public function mouse_pressed(e:MouseEvent):void {
drawing=true;
canvas.graphics.moveTo(mouseX,mouseY);
points_array=new Array();
saved_x=mouseX;
saved_y=mouseY;
points_array.push(saved_x);
points_array.push(saved_y);
}
public function mouse_moved(e:MouseEvent):void {
if (drawing) {
var dist_x:int=mouseX-saved_x;
var dist_y:int=mouseY-saved_y;
if (dist_x*dist_x+dist_y*dist_y>pixel_dist*pixel_dist) {
canvas.graphics.lineTo(mouseX,mouseY);
saved_x=mouseX;
saved_y=mouseY;
points_array.push(saved_x);
points_array.push(saved_y);
}
}
}
public function mouse_released(e:MouseEvent):void {
drawing=false;
var sx:int;
var ex:int;
var sy:int;
var ey:int;
var dist_x:int;
var dist_y:int;
var dist:Number;
var angle:Number;
var segments:int=points_array.length/2-1;
for (var i:int=0; i
and this is the result:
draw with the mouse and press SPACE to play/pause the simulation.
Next step, chalk management and goal.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.