问题
As my research goes everywhere it's said that we should only use one sprite batch per instance. But what if i set sprite batch to static in class which controls the game (calls different screens etc).
When i have tryed this its working like charm, but is that a good practice or it may result some problems in future. Then if it could be done can i also share stage and shape renderer?
note: the main reason why i am trying to go with that "static batch technique" is that java crashes when i try to dispose stage, sprite batch or shape renderer
回答1:
I do it that way as well and I didn't run into any problems so far.
You need to be careful with the SpriteBatch.begin()
and SpriteBatch.end()
calls, since you may never know where it was started already, and where it was stopped when it is shared.
Same applies for the Camera
/ProjectionMatrix. And also for the currently used ShaderProgram
in case you are working with shaders.
Make sure that you are always fully aware of where you are doing what with your SpriteBatch
otherwise you might face weird bugs one day.
Stage
should not be shared, since it will probably serve different purposes. For different UIs it doesn't make any sense to use only one Stage
.
ShapeRenderer
could be shared, since it kind of works the same like SpriteBatch
, but probably you only want to use it in a single place.
dispose()
should actually work without any problems. You have to make sure that you dispose everything in the correct order so nothing will be left over in the end and cause exceptions.
回答2:
As @noone allready said it should not cause any problems, if you take care.
But i just wanted to point out another thing: In some Screen
s (MenuScreen
, OptionScreen
, ...) you may use Scene2dUi
for some UI stuff. So you will use Stage
s, which belong to the Screen
and are not shared. So in this case you should remember, that every Stage
has its own SpriteBatch
and Camera
. So it could be, that you only need an additional SpriteBatch
in your
GameScreen
. If this is the case i would not make it static and have it only in the GameScreen
.
As noone said dispose()
should work, if you use the correct order, and so also going with more then 1 SpriteBatch
should not cause any problems.
So i think both ways will work and are good approaches. It depends on your design and how/where you want to use the SpriteBatche
s and Camera
s.
来源:https://stackoverflow.com/questions/22121694/static-sprite-batch