保存精灵位置

Saving Sprite Position

我有一个精灵,当到达某个位置时它会被移除:

 if(sprite.getY()>=700){
      enemyIterator.remove();
      Pools.free(sprite);
}

我不想在精灵被移除之前保存它的最后位置,我尝试了 sprite.getX()sprite.getY() 但它们只能在精灵在游戏中时使用。

也许你无法获取精灵最后的位置,但你可以保存你在这一帧中绘制的最后位置,你总是需要在绘制每一帧时设置一个位置,如果发生什么情况,赶上最后一个位置,例如 .

private Rectangle bordes;

public Ball(float x, float y,int isMoved) {
    ..........
        texture = new Texture(Gdx.files.internal("bola.png"));
        bordes = new Rectangle(x, y, texture.getWidth(), texture.getHeight());
    ..........
    }

public void draw(SpriteBatch batch) { 
    //where i draw my last texture or sprite  was bordes.x bordes.y in this frame
        batch.draw(texture, bordes.x, bordes.y, texture.getWidth(), texture.getHeight());
    }

public vois getLastPosition(){

lastX = bordes.x;
lastY = bordes.y;

}

基于提供的附加信息。 Sprite 方法 getX()getY() returns 浮点值。所以你可以将这些方法返回的值赋值给float类型的变量,以备后用。

float lastX, lastY;

if(sprite.getY()>=700){
  lastX = sprite.getX();
  lastY = sprite.getY();
  enemyIterator.remove();
  Pools.free(sprite);
}

System.out.println("Removed sprite coordinates where: " + lastX + ", " + lastY);