nape remove body in collision

892 views Asked by At

I have a function that detects when the objects collide but how do I delete one of them?

private function fruitToFloorCollision(collision:InteractionCallback):void
        {
            // TODO Auto Generated method stub

            trace('fruit hits floor');

        }

when I trace out the collision

Cb:BEGIN:(dynamic)#11/(static)#1 : [CollisionArbiter(Polygon#2|Circle#12)[SD]<-ACCEPT] : listener: InteractionListener{BEGIN#COLLISION::@{[CbType#5] excluding []}:@{[CbType#6] excluding []}} precedence=0
3

There are 3 answers

0
Monster Brain On

As far as i know, Considering you have 2 Objects fruit and floor, interactionListener added in that order.

private function fruitToFloorCollision(collision:InteractionCallback):void
    {

        // CBTypes are added to Shapes not Bodies. So collision.int1 is a shape.

        var fruit:Body = collision.int1.castShape.body as Body;
        removeChild(fruit.userData.graphic);
        space.bodies.remove(fruit);
    }
0
LeBlaireau On
my answer.....

if anyone has got a better/alternative would like to hear it...

private function fruitToFloorCollision(collision:InteractionCallback):void
        {


            var ball:Body = collision.int1 as Body;
            removeChild(ball.userData.graphic);
            space.bodies.remove(ball);
        }
0
reanimated On

The preferred idiom with Nape to remove the body from the simulation seems to be:

ball.space = null;

Although it internally calls space.bodies.remove() just as you did, it also does some extra checks as well.