Sprite Deletion

Problem

I want to remove my Sprite as part of callback (IAnimationListener, IShapeModifierListener). When I try to do that my code crashes.

Solution

You must schedule your code to run in an update thread of AndEngine.

So instead of removing your sprite directly like this:

          final TiledTextureRegion region = AssetsManager.poofRegion.clone();
          final AnimatedSprite poof = new AnimatedSprite(10, 20, region);
          crittersLayer.addEntity(poof);
          poof.animate(ANIMATION_POOF_FRAME_TIME, 0,
            new AnimatedSprite.IAnimationListener() {
              public void onAnimationEnd(AnimatedSprite pAnimatedSprite) {
                crittersLayer.removeEntity(poof);
              }
            }
          );

Do:

          final TiledTextureRegion region = AssetsManager.poofRegion.clone();
          final AnimatedSprite poof = new AnimatedSprite(10, 20, region);
          crittersLayer.addEntity(poof);
          poof.animate(ANIMATION_POOF_FRAME_TIME, 0,
            new AnimatedSprite.IAnimationListener() {
              public void onAnimationEnd(AnimatedSprite pAnimatedSprite) {
                // Activity is BaseGameActivity
                activity.runOnUpdateThread(new Runnable() {
                  public void run() {
                    crittersLayer.removeEntity(poof);
                  }
                });
              }
            }
          );

Based on forum thread

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License