Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GameStateManager LibGDX

Tags:

libgdx

box2d

I started a project with libgdx and I have a GameStateManager for my GameStates Menu and Play.

If I run the project it shows the Menu and then I can click a button to open the Play GameState. The Problem is, that when I ended the Game it should show the Menu State again, but I get a black screen. I tested if the render() method is started (with System.out...) and the render() method in Menu is starting.

I am not shure why I get a black screen when I "reopen" the Menu state. Maybe its not working because I use Box2D in Play but I dont know.

Here some code:

This is the method in Play which should open the Menu if the player is at the end:

public void playerEnded() {

    gsm.setState(GameStateManager.MENU);

}

Maybe you can tell me, if I have to end box2d things or so. I hope someone can help me, and if you want more code - no problem.

like image 289
ClashOfClansPhil Avatar asked Nov 25 '25 15:11

ClashOfClansPhil


1 Answers

Your custom GameStateManager should extend this class:

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/Game.html

To change screens you should be using Game.setScreen(Screen screen)

Each different screen should be an implementation of Screen.

So the way it works in my libGDX projects is such that GameScreen extends Screen, and MenuScreen extends Screen. That way I can change what draws on what screen.

This all goes back to interfaces and polymorphism, so if you don't get those concepts, just give it a quick google and you'll get an idea what you need to do.

like image 61
Scuba Steve Avatar answered Nov 28 '25 15:11

Scuba Steve