Question

In LibGdx SpriteBatch doc. it's mentionned that

A SpriteBatch is a pretty heavy object so you should only ever have one in your program.

But i'm confused a little ! I want to know if i have to maintain one SpriteBatch in all the Game(as singleton) or have one SpriteBatch per Screen as shown in several tutorials that i find till now.

I also want to know if is a good idea to keep a SpriteBatch as singleton.

Thank you in advance.

Was it helpful?

Solution

You can get some informations here.

In my opinion you can have 1 or even 2 SpriteBatchs per Screen. If you set another Screen as your current the hide() method is called, and there you can simply dispose() your SpriteBatch.
This means, that you have to create a new one in the show() method.
You can use one SpriteBatch for game and one for HUD/UI without any problems. But always make sure you call end() on one before calling begin() on the other, so that only one SpriteBatch is active at a time.
Of course if you don't need more then one you should not use more then one. And if you want you can also have one for the whole gmae. But be carefull with the different ProjectionMatrixes you may use for different screens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top