Search code examples
javalibgdxpositionsprite

Saving Sprite Position


I have a sprite that when reaches a certain position it gets removed:

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

I wan't to save the last position of the sprite before it gets removed,I tried sprite.getX() and sprite.getY() but those are only usable while the sprite is in the game.


Solution

  • Based on provided additional information. Sprite methods getX() and getY() returns float values. So you can assign values returned by these methods to variables of type float for later use.

    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);