Libgdx screens do not change

I'm new to Libgdx, and I was written a class that extends the Game class, the fact is that the setScreen () method from Game does not change screens, because after setting the screen, my game still displays only what is in the rendering method from the game class, not what is in the screen class rendering method. This is the code:

If I run this code, I get only a red screen, even if I change the screens when the user touches the screen

class myGame extends Game { GameScreen myOtherScreen; public void create() { //create other screen myMenuScreen = new GameScreen(); } public void render(float delta) { // change screens if screen touched if(Gdx.input.justTouched()) setScreen(myOtherScreen); //render red screen Gdx.gl.glClearColor(1, 0, 0, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); } . . //other methods . } // ======= Screen Class ======== public class GameScreen implements Screen { @Override public void render(float delta) { //render green screen Gdx.gl.glClearColor(0, 1, 0, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); } . . //other methods . } 
+4
source share
3 answers

You are not using the game class correctly. You should not do any rendering there that task screens.

You should see the page page and page of the screen widget and game classes . Use should be something like this:

 public class MyGame extends Game { @Override public void create() { setScreen(new RedScreen(this)); } } 

and have RedScreen as follows:

 public class RedScreen implements Screen { MyGame game; public RedScreen(MyGame game){ this.game = game; } public void render(float delta) { if(Gdx.input.justTouched()) game.setScreen(new GreenScreen(game); //render red screen Gdx.gl.glClearColor(1, 0, 0, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); } // ... more screen functions } 

and have GreenScreen as follows:

 public class GreenScreen implements Screen { MyGame game; public MainMenuScreen(MyGame game){ this.game = game; } public void render(float delta) { //render green screen Gdx.gl.glClearColor(0, 1, 0, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); } // ... more screen functions } 
+8
source

the problem found is not to use the rendering method from the game class, in fact, you really have to REMOVE THIS from the class, trust me that you should do all the rendering in other screen classes.

+5
source

I found the main reason. In fact, you should add super.render() to your Game render() method before drawing other things in the game. If not, the rendering method that you redefined will not call the Screen render() method.

BTW: Best practice is to render things using the Screen class.

Note that: Even an empty render() in the game causes this problem. So, remove render() in your game or add super.render() to it.

+3
source

All Articles