Class StageManager
This class is responsible for handling transitions between various stages, such as the main menu, gameplay,
settings, etc. It employs a singleton pattern to ensure only one instance exists throughout the game's lifecycle.
The StageManager must be initialized via create(Game) before it can be accessed using getInstance().
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StageManagercreate(com.badlogic.gdx.Game game) Creates a newStageManagerinstance with the specifiedGameinstance.Returns the current stage (screen) being displayed by the game.static StageManagerReturns the singleton instance of theStageManager.voidsetNextStage(Stages nextStage, Object... params) Sets the next stage to be displayed along with any parameters required to build it.voidshowScreen(Stages stage, Object... params) Displays the screen corresponding to the given stage.voidupdate()Checks if a next stage has been set and, if so, transitions to that stage.
-
Constructor Details
-
StageManager
public StageManager()
-
-
Method Details
-
getInstance
Returns the singleton instance of theStageManager.This method throws an
IllegalStateExceptionif theStageManagerhas not been created yet.- Returns:
- the singleton
StageManagerinstance - Throws:
IllegalStateException- if theStageManageris not initialized
-
create
Creates a newStageManagerinstance with the specifiedGameinstance.This method must be called before any other calls to
StageManager.getInstance(). If aStageManagerinstance already exists, anIllegalStateExceptionis thrown.- Parameters:
game- theGameinstance to associate with thisStageManager- Returns:
- the newly created
StageManagerinstance - Throws:
IllegalStateException- if aStageManageris already initialized
-
showScreen
Displays the screen corresponding to the given stage.This method retrieves the new stage from the
Stagesenum using the provided parameters, builds the stage with anInputMultiplexer, sets it as the current screen in theGame, and disposes the previous stage if it exists.- Parameters:
stage- theStagesenum value representing the stage to be shownparams- optional parameters required to build the stage
-
setNextStage
Sets the next stage to be displayed along with any parameters required to build it.The specified stage and parameters will be applied on the next call to
update().- Parameters:
nextStage- theStagesenum value representing the next stage to displayparams- optional parameters required by the stage's screen builder
-
update
public void update()Checks if a next stage has been set and, if so, transitions to that stage.If a next stage is defined, this method calls
showScreenwith the provided stage and parameters, and then resets the next stage variables.
This is done because the context in whichshowScreencan be called doesn't always have the required libGDX context to draw in the application window. This means that update should only be called from within the scope of the Game class. -
getCurrentStage
Returns the current stage (screen) being displayed by the game.- Returns:
- the current
AbstractStageornullif no stage is currently set
-