Android : Crash app when destroyBody in andengine

on Sunday, August 3, 2014


I have a simple game with a player and some coins.in ContactListener when player contact whit coin catch it and remove coin.it's my code and it's work very good in blue stacks but in phone crashed.


in ContactListener :



if (a.getUserData() != null && b.getUserData() != null) {
if (a.getUserData().equals("player") && b.getUserData().equals("coin")
|| b.getUserData().equals("player") && a.getUserData().equals("coin")) {

resourceManager.coinCollect.play();

score+=5;
scoreText.setText("Coins : "+score);

if(a.getUserData().equals("coin")){
removeBody(a);
}else if(b.getUserData().equals("coin")){
removeBody(b);
}
}

}


Remove body :



private void removeBody(final Body body1){

for(int i=0;i<coins.size();i++){
if(coins.get(i).getUserData()==body1){
detachChild(coins.get(i));
}
}

activity.runOnUiThread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
mPhysicsWorld.destroyBody(body1);
}
});

}

0 comments:

Post a Comment