user2005938
user2005938

Reputation: 179

AndEngine - Destroy body from scene class?

I have a class that extends scene, which controls the game. In my ContactListener I have:

if (x1.getBody().getUserData().equals("wall")
                        && x2.getBody().getUserData().equals("arrow")) {
                    System.out.println("arrow is x2");
                }

When the arrow, x2 collides with wall, x1, I want the arrow body to be destroyed. They collide, but of course I can't destroy it from ContactListener. Since this class extends scene how could I either get an update or run method in it (all the other posts I've seen on this had classes which have extended BaseGameActivity), or destroy the x2 body otherwise?

Upvotes: 0

Views: 421

Answers (1)

Siddharth
Siddharth

Reputation: 4312

I have a solution for you.

When you create a scene pass a BaseGameActivity reference to it. Like in the following

new GameScene(MainActivity mainAcivity);

Then after remove box2d object you have to use this reference.

mainActivity.runOnUpdateThread(){  
    public void run(){
          mPhysicsWorld.destroyBody(arrowBody);
    }
}

Upvotes: 2

Related Questions