LibGDX Stage vs SpriteBatch to draw the Game
Asked Answered
E

2

6

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?

Enharmonic answered 9/3, 2014 at 15:54 Comment(0)
C
21

Actually you always draw with SpriteBatch, as Stage uses its own SpriteBatch. If you think you could need some of the advantages of Scene2ds Stage, like Actions, Groups 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 Drawables so you will have some problems with Animations), 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 Actors, and when updateor 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.

Cathee answered 10/3, 2014 at 7:42 Comment(2)
Second awesome answer, but much more details, which help me a lot ;)Krefeld
I use Scene2D simply so that I can use the EventListener with little to no overhead.Condign
B
16

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)

Bhagavadgita answered 9/3, 2014 at 16:0 Comment(2)
I 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.Vue
@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)Bhagavadgita

© 2022 - 2024 — McMap. All rights reserved.