0
\$\begingroup\$

Well, i'm facing a strange problem with my code. I'm trying to remove a sprite named ice when either of player, enemy or the ground comes in contact with the ice. The contact is made successfully and the ice is hidden after the contact but due to some reasons the player,enemy and the ground still comes in contact with the ice. I want the ice object to be completely removed from the scene when contact is made. I'm using the below code:

public void beginContact(Contact contact) { // TODO Auto-generated method stub Fixture x1 = contact.getFixtureA(); Fixture x2 = contact.getFixtureB();

if (x1.getBody().getUserData().equals("player")
        && x2.getBody().getUserData().equals("ice") && gb.isVisible()==true) {
    Log.i("Contact Made", "CN");
    detachChild(gb);
    gb.setVisible(false);
    score = score + 1;
    scoreText.setText("Score: " + score);
}
if (x1.getBody().getUserData().equals("ground")
        && x2.getBody().getUserData().equals("ice") && gb.isVisible()==true) {
    detachChild(gb);
    gb.setVisible(false);

}
if (x1.getBody().getUserData().equals("enemy")
        && x2.getBody().getUserData().equals("ice") && gb.isVisible()==true) {
    detachChild(gb);
    gb.setVisible(false);

}

}

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You are detaching the sprite, but not the physics body. The physics body and sprite are connected by the physics connector which basically just moves and rotates the sprite based on the body's position and rotation. You have to either destroy the body or set it as not active.

mPhysicsWorld.destroyBody(body);

or

body.setActive(false);

Both will do. Destorying a body in AndEndengine must be done at a specific time (at the beginning of the next loop cycle is the best) otherwise you might get some errors.

Here's a link to another stackoverflow question that deals with it.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .