I’m using AndEngine/Box2D to create a game. When the game loads, an array of a class containing sprites and bodies are created. When the user touches the screen, the next sprite in order is attached to the scene and it’s body is set to active. The sprite/body can later be destroyed. To do this, I detach the sprite and set the body to inactive. However, at that point, the scene no longer registers a touch. When dragging a sprite/body into the path, it stops. However, other bodies on the scene do not interact with it. This leads me to believe it has something to do with a touch error. Here is my code:
private void destroyFiller(){ //Deletes filler
if(filler[fillerNum].active){
filler[fillerNum].active=false;
mPhysicsWorld.unregisterPhysicsConnector(mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(filler[fillerNum].sprite));
filler[fillerNum].body.setUserData("destroy");
scene.detachChild(filler[fillerNum].sprite);
fillerCount--;
fillersLeftText.setText("Balls left: "+Integer.toString(fillerCount));
if(fillerCount==0)
gameOver();
}
}
and where it sets the body to inactive
scene.registerUpdateHandler(new IUpdateHandler() {
@Override
public void reset() {
}
@Override
public void onUpdate(float pSecondsElapsed) {
if(fillerNum>-1){
if(filler[fillerNum].body.getUserData().equals("destroy")){ //"Destroys" the body which can only be done on an update
filler[fillerNum].body.setActive(false);
filler[fillerNum].body.setUserData("destroyed");
if(soundOn)
popSound.play();
}
This is the scene touch event:
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
if(this.mPhysicsWorld != null && mEngine != null) {
if(pSceneTouchEvent.isActionDown()) {
Log.e("SceneTouchEvent","Touch"); //This line doesn't execute when touching a body that passed through destroyFiller
createFiller(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
}
return false;
}
And createFiller if needed:
private void createFiller(float x, float y) {
fillerNum++;
filler[fillerNum].active=true;
filler[fillerNum].sprite.setPosition(x-fillerTR.getWidth()/2,y - fillerTR.getHeight()/2);
scene.registerTouchArea(filler[fillerNum].sprite);
filler[fillerNum].body.setUserData("fill");
filler[fillerNum].body.setActive(true);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(filler[fillerNum].sprite, filler[fillerNum].body, true, true));
scene.attachChild(filler[fillerNum].sprite);
}
try to use unregisterTouchArea in your deleting method
also try to use it in onUpdateThread