这是我的第一次尝试:
.......
.......
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 中引发 IndexOutOfBoundsException -线!
那么当我的播放器过来时我如何删除精灵呢?
ps:我看过 SpriteRemoveExample,但在我的情况下它对我没有帮助