Talking about Actionscript 3 and Flash.
With the increasing popularity and development of NAPE, which now reaches version 2.0, it’s time to start looking at it and writing some tutorials.
I already showed you a quick comparation between Box2D and Nape in Box2D Vs Nape: Hello both worlds, today I am showing you how to create and destroy a box with a mouse click, and how to skin bodies.
So if you click with the mouse on this movie you will:
* Create a crate if you clicked on an empty space
* Destroy a crate if you clicked on it
And the whole thing is managed by this script:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import nape.geom.Vec2;
import nape.phys.Body;
import nape.phys.BodyList;
import nape.phys.BodyType;
import nape.shape.Polygon;
import nape.space.Space;
import nape.util.ShapeDebug;
public class Main extends Sprite {
// world creation with gravity
private var world:Space=new Space(new Vec2(0,500));
public function Main() {
// static body creation with origin at pixels 320,500
var staticFloor:Body=new Body(BodyType.STATIC,new Vec2(320,500));
// new 640x40 box creation
var floor:Polygon=new Polygon(Polygon.box(640,40));
// elasticity property, similar to Box2D restitution
floor.material.elasticity=0.5;
// adding polygon to body
staticFloor.shapes.add(floor);
// adding body to world
staticFloor.space=world;
addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.CLICK,handleClick);
}
private function handleClick(e:MouseEvent):void {
// getting all bodies at a given point (mouse coords)
var bodies:BodyList=world.bodiesUnderPoint(new Vec2(mouseX,mouseY));
// if any...
if (bodies.length>0) {
for (var i:int = 0; i < bodies.length; i++) {
// getting the body
var body:Body=bodies.at(i);
// destroying it and removing its graphic asset if dynamic
// we don't want to remove the floor although it's outside the stage
if (body.type==BodyType.DYNAMIC) {
removeChild(body.userData.sprite);
world.bodies.remove(body);
}
}
}
else {
// creation of a dynamic body, same thing as the static floor
var dynamicBox:Body=new Body(BodyType.DYNAMIC,new Vec2(mouseX,mouseY));
var block:Polygon=new Polygon(Polygon.box(50,50));
block.material.elasticity=0.5;
block.material.density=1;
dynamicBox.shapes.add(block);
// setting its custom data
dynamicBox.userData.sprite=new Crate();
addChild(dynamicBox.userData.sprite);
dynamicBox.space=world;
}
}
private function update(e:Event):void {
world.step(1/30,10,10);
// looping though bodies
var bodies:BodyList=world.bodies;
for (var i:int = 0; i < bodies.length; i++) {
var body:Body=bodies.at(i);
if(body.userData.sprite!=null){
// adjusting graphic asset position
body.userData.sprite.x=body.position.x
body.userData.sprite.y=body.position.y
body.userData.sprite.rotation=body.rotation*57.2957795;
}
}
}
}
}
Set up your NAPE project using the swc in the official download page and start playing with it.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.