# Adding, removing and skinning bodies with NAPE

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;
staticFloor.space=world;
}
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;
// setting its custom data
dynamicBox.userData.sprite=new Crate();
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){
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.

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 10000000
// 2 Cars
// 2048
// Avoider
// Ballz
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Breakout
// Bricks
// Columns
// CubesOut
// Dots
// DROP'd
// Dudeski
// Eskiv
// Filler
// Fling
// Globe
// HookPod
// Hundreds
// InkTd
// Iromeku
// Lumines
// Magick
// MagOrMin
// Maze
// Memdot
// Nano War
// Nodes
// o:anquan
// Ononmin
// Pacco
// Phyballs
// Platform
// Poker
// Pool
// Poux
// Pudi
// qomp
// Racing
// Renju
// SameGame
// Security
// Sling
// Slingy
// Sokoban
// Splitter
// Sproing
// Stack
// Stairs
// Stringy
// Sudoku
// Tetris
// Threes
// Toony
// Turn
// TwinSpin
// vvvvvv
// Wordle
// Worms
// Yanga
// Zhed
// zNumbers