LibGDX Stage vs SpriteBatch to draw the Game
Solution 1
Actually you always draw with SpriteBatch
, as Stage
uses its own SpriteBatch
. If you think you could need some of the advantages of Scene2d
s Stage
, like Action
s, Group
s or other things, you should use Stage
. In my opinion it is really easy to use. You have your own objects, which extend Image
or Actor
, override their update(delta)
methods, where you check if the Actor
should move (Keypressed or an event for the AI). Also you should detect collisions there and take care about them. Then, if you are extending Image
you don't need to care about drawing (but Image
needs Drawable
s so you will have some problems with Animation
s), if you are extending Actor
override the draw(SpriteBatch batch)
, where you call batch.draw(...)
.
Thats it.
A big disadvantage of Stage
is the sorting. The Actor
, which is drawn as first is in the background. The last Actor
overdraws all others and is in foreground. You can sort them by using the z-Index, but it is not really sure.
So you may decide to use your own kind of Stage
(it is more or less a list, containing all Actor
s, and when update
or draw
is called, it is called for every Actor
in this list), and add your own sorting there.
It depends on your game design, on your opinion and on your style. Look at some tutorials/examples and decide how you want to create your game.
Solution 2
You can use either, it depends on what kind of game are you creating. But anyway, don't create multiple instances of SpriteBatch
— instead create only one and pass it to Stage
constructor, because it's heavy object. (Yes, Stage
uses a SpriteBatch
under the hood)
Also, Stage
may be extremely useful if you need any on-screen controls (buttons, joystick, etc), because you can use classes from scene2d.ui
and don't reinvent the wheel (some wheels are not very easy to reinvent properly)
Fabian König
Updated on June 08, 2022Comments
-
Fabian König almost 2 years
I have a big problem with the drawing in LibGDX. First I don´t use every physics or other complicate things in my game. I only draw sprites and put Rectangles at this for Collision Detection. I have 3 Screens in my game: splash, menu and game. So I use a Stage in the splash and menu screen but I don´t really if I use Stage in the game too or I draw with SpriteBatch? The Stage have really pros with Actions but it a little bit more complicate to use Actors. Are there other ways to do this? I don´t want to use World, because its to complicate for the beginning of game developing. What I want to use for the game?
-
Fabian König about 10 yearsSecond awesome answer, but much more details, which help me a lot ;)
-
Buddy over 8 yearsI have a question, is doing multiple batch.begin() and batch.end() costly ? I am asking because after you are done with spritebatch, you have to call batch.end() and in the source code of Stage, it calls batch.begin() and batch.end() again.
-
Display Name over 8 years@EnesBattal I can't give an exact answer. It is definitely costly, because batch.end() presumably sends the commands to GPU, but in practice it doesn't hurt too much if used 2 times per frame, and in this case it's a necessary evil (otherwise one needs to use more complex abstractions, or do all drawing on lower level)
-
Jax about 8 yearsI use Scene2D simply so that I can use the
EventListener
with little to no overhead.