Here's my case. I have three different types of screens:
Am I better off storing my Screens in variables and reusing them when I switch screens, or is it better to dispose of each Screen when I'm done with it and then create a new instance of the screen type I intend to switch to?
And would the answer be different in a case where Screen switching is more frequent (like between an overworld Screen and a battle Screen in a game like Final Fantasy or Pokemon)?
Thanks!
In my opinion you should reuse Screen
s, which are used frequently and dispose and recreate Screen
s not used that often.
In your example the GameScreen
, GameOverScreen
and MainMenuScreen
should all be reused, as they are directly connected to each other:
As soon as a player starts a game, it is possible, that he dies. Then the GameOverScreen
is shown and directly after that the MainMenuScreen
is shown. So the user does not need to switch to those Screen
s manually.
The OptionsScreen
instead, could be disposed and recreated every time, because usualy you open the OptionScreen
only a few times. Also it is not directly connected to any other Screen
, but the user needs to "force" the game to open the OptionScreen
.
But as long as there aren't too many Screen
s, reusing shouldn't be a problem.
In your second example, the Screen
s are used even more often, so they definitly should be reused.
EDIT:
As @EssEllDee mentioned, you should also make use of hide()
and show()
. Those methods get called by Libgdx, when you switch the Screen
and they can be used to dispose/recreate heavy ressources. For example, you might dispose Texture
s used for the game, when you switch to the GameOverScreen
and reload them, when switching to the GameScreen
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With