LeBlaireau
LeBlaireau

Reputation: 17467

nape remove body in collision

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

Upvotes: 0

Views: 868

Answers (3)

Monster Brain
Monster Brain

Reputation: 2119

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);
    }

Upvotes: 0

reanimated
reanimated

Reputation: 11

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.

Upvotes: 1

LeBlaireau
LeBlaireau

Reputation: 17467

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);
        }

Upvotes: 0

Related Questions