这是我的第一次尝试:

.......
.......
OTHER CODE
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
private  class Ball extends AnimatedSprite {
private final PhysicsHandler mPhysicsHandler;

public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
    super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
    this.mPhysicsHandler = new PhysicsHandler(this);
    this.registerUpdateHandler(this.mPhysicsHandler);
}

@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
    /* Get the scene-coordinates of the players feet. */
    final float[] playerFootCordinates = this.convertLocalToSceneCoordinates(16, 16);

    int foodX = ((int) playerFootCordinates[Constants.VERTEX_INDEX_X]) / 20;
    int foodY = ((int) playerFootCordinates[Constants.VERTEX_INDEX_Y]) / 20;


    final TMXTile tmxTile = tmxLayer.getTMXTileAt(playerFootCordinates[Constants.VERTEX_INDEX_X], playerFootCordinates[Constants.VERTEX_INDEX_Y]);

    if (tmxTile.getGlobalTileID() == 2){
        final EngineLock engineLock = mEngine.getEngineLock();
        engineLock.lock();

        /* Now it is save to remove the entity! */
        scene.detachChild(food[foodX][foodY]);
        food[foodX][foodY].dispose();
        food[foodX][foodY] = null;

        engineLock.unlock();
    }
      // OTHER CODE
       ......
 }


但它不起作用(“ java.lang.IndexOutOfBoundsException:无效的位置blabla,大小为blabla”

我读过:
警告:应从已注册到场景或引擎本身的RunnableHandler.postRunnable(Runnable)内部调用此函数(detachChild),否则可能会在Update-Thread或GL-Thread中引发IndexOutOfBoundsException!

所以当我的播放器过来时,我如何删除精灵?

ps:我看过SpriteRemoveExample,但对我来说并没有帮助

最佳答案

通过这种方式,您可以安全地删除您的Sprite,不会触发任何ArrayIndexOutOfBoundException。

    mActivity.runOnUpdateThread(new Runnable() {

        @Override
        public void run() {
            clearUpdateHandlers();
            clearEntityModifiers();
            clearTouchAreas();
            detachSelf();
        }
    });

关于android - 当播放器过来时删除Sprite,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17002369/

10-12 00:20
查看更多