这是我的第一次尝试:
.......
.......
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”
我读过: 警告:此函数 (detachChild) 应从注册到场景或引擎本身的 RunnableHandler.postRunnable(Runnable) 中调用,否则它可能会在 Update-Thread 或 GL-Thread 中抛出 IndexOutOfBoundsException!
那么当我的玩家过来时我该如何移除 Sprite 呢?
ps:我看过 SpriteRemoveExample,但它对我的情况没有帮助
最佳答案
按照以下方式您可以安全地删除 Sprite ,不会触发任何 ArrayIndexOutOfBoundException。
mActivity.runOnUpdateThread(new Runnable() {
@Override
public void run() {
clearUpdateHandlers();
clearEntityModifiers();
clearTouchAreas();
detachSelf();
}
});
关于android - 当 Player 过来时移除 Sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17002369/