如何使AnimatedSprite播放一次然后删除自己?

时间:2011-08-22 17:13:02

标签: android sprite andengine

我有一个方法makeExplosion

private void makeExplosion(float x, float y){
    final AnimatedSprite explosion = new AnimatedSprite(x, y, BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mExplosionBitmapTextureAtlas, this, "explosion.png", 0, 0, 4, 2)); 
    explosion.animate(50);
    mScene.attachChild(explosion);
    explosion.registerUpdateHandler(new IUpdateHandler(){

        @Override
        public void onUpdate(float pSecondsElapsed) {
            if(explosion.getCurrentTileIndex() == 7){
                mScene.detachChild(explosion);
            }
        }

        @Override
        public void reset() {

        }});
}

..哪个不起作用。我收到以下错误:

FATAL EXCEPTION: UpdateThread
java.lang.IndexOutOfBoundsException: Invalid index 19, size is 19
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
    at java.util.ArrayList.get(ArrayList.java:311)
    at org.anddev.andengine.entity.Entity.onManagedUpdate(Entity.java:1024)
    at org.anddev.andengine.entity.scene.Scene.onManagedUpdate(Scene.java:250)
    at org.anddev.andengine.entity.Entity.onUpdate(Entity.java:882)
    at org.anddev.andengine.engine.Engine.onUpdateScene(Engine.java:488)
    at org.anddev.andengine.engine.Engine.onUpdate(Engine.java:483)
    at org.anddev.andengine.engine.Engine.onTickUpdate(Engine.java:459)
    at org.anddev.andengine.engine.Engine$UpdateThread.run(Engine.java:685)

...我假设是因为我正在删除其AnimatedSprite中间的onUpdate()。如果没有这个问题,我怎样才能实现我的目标?

1 个答案:

答案 0 :(得分:5)

从场景中分离精灵时,它应该在runOnUpdateThread()中分离;

    public void onUpdate(float pSecondsElapsed) {
        if(explosion.getCurrentTileIndex() == 7){
            runOnUpdateThread(new Runnable() {
            @Override                
            public void run() {
              mScene.detachChild(explosion);
            }
           });                
        }
    }