Search code examples
libgdx

Libgdx Individually reference textures


In my game I have some gold that the player can collect. My problem is I want to individually refernce textures of gold so i can take that particular instance of gold off the screen (player picked it up). How would I go about doing this as i am pretty sure you cannot do this with regular textures. Would i have to create a shape and fill it with the gold texture so I can delete that particualr gold piece? Thanks


Solution

  • I think you confuse Texture (which is basically a loaded image that you can draw) with game entities. Depending on how you implement your game, you can spawn multiple bodies (Box2D), actors (Scene2D) or your simple data containers (width, height, x, y) representing each coin and draw each one on the screen using the same texture. Actually, that's the preferred way to handle assets: you load images once and then simply reuse them for each sprite.

    I suggest looking into the following classes:

    • Sprite (makes it easier to draw textures).
    • Image (allows to display simple images on Scene2D stage).
    • Box2DSprite (third party utility class that makes it easier to draw Box2D worlds).