# How can SpriteBatch use a single texture/asset as multiple independent objects/instances

I'm using LibGDX to create a game, but I'm encountering a problem with SpriteBatch. Whenever two objects that use the same image for their sprite come onto the screen, the new object replaces the old object. So, for example, one ship will come onto the screen, and when a ship with the same sprite comes onto the screen, the old ship will disappear and the new ship will have all the damage and other characteristics of the old ship.

I could always make multiple copies of the same image and put them in the assets folder, but that seems unnecessary. Does anyone have any ideas? Am I using SpriteBatch incorrectly?

Edit: Here's a couple of examples of rendering methods in the game loop.

The method for rendering ships:

private void renderDoodad(Doodad doodad) {

batch.begin();


Followed by some code about what to do if the ship is the player's ship. But the player's ship is rendering fine, so I left that out.

Then there's the method for rendering shots:

    private void renderShot(Shot shot) {
if (!shot.isDisabled()) {
batch.begin();

batch.draw(
shot.getSprite(),
shot.getX(),
* getApp().getGraphics().getDeltaTime()));
batch.end();
}
}

• Could you please show some more code to give more context as what you've posted leaves many things unanswered. For example, how do you update the sprite when you move the associated object, are you using the libgdx Sprite class, etc. Also, you don't need to call batch.begin() and batch.end() around every single batch.draw() call. One call to batch.begin() when drawing starts, and one call to batch.end() when all drawing ends should suffice for most cases. Mar 13 '13 at 23:22
• On a side note: beginning and ending a spritebatch with every sprite defeats the purpose of having a spritebatch at all. Call batch.begin() when you start drawing a group of sprite that use the same resources then use batch.draw() for each one. Only call batch.end() after you are done drawing one group and need to either use another texture or end your draw step. Mar 18 '13 at 14:46
• I'm not sure why you think I'm using batch.begin() and end() improperly. Are you suggesting I somehow make the batch continue even once the method ends? Mar 19 '13 at 3:45

I can't see how rendering (another ship is being rendered) can cause changes in game logic - damage and other ship characteristics, as you said. If this was truly a sprite rendering issue, the game objects would be fine and only the graphics would be messed up.

It seems to me more like some reference mistake. Is it possible that you accidentally use "x = y" instead of "x.set(y)"? That will bond objects together instead of copying one object's properties to another. You can check that in debugger. If you inspect your game objects, they should generally have different pointer (the number in parenthesis after the variable name). Only used SpriteBatch or Texture/Region should point to the same object.

If I am wrong, you should probably edit your question and add some example of your code. There should be no issues in drawing multiple Sprites which share the same Texture. It is being used in numerous games. Also check SpriteBatch wiki page for code snippets.

It's hard to tell without seeing your code (show some!), but I'm guessing that your draw order may be messed up. Maybe you are clearing your buffer each time you draw a new sprite or maybe you are drawing your background again over top of the old one. Post code that is relevant to your game loop's draw step and shows how you are using SpriteBatch.

• I was a bit hesitant to post code, just because rendering is spread out over a number of methods and arrays. I've attached a couple of the relevant methods, though, for you and @David to look at. Mar 11 '13 at 1:02
• I didn't realize you had said this before but is is crucial: " the old ship will disappear and the new ship will have all the damage and other characteristics of the old ship." This implies that you are referencing your ships wrongly. Instead of creating a new separate one, you are using an existing one. Look closer at how you create new ships. Mar 18 '13 at 14:43

I'm a couple of days late in answering this, but I thought I'd let you know. The problem was with Sprites being referenced through a static helper class and therefore not instantiating properly. This has been fixed by switching the helper class to textures.