I am new to Libgdx and I was wrote a class that extends the Game class, the thing is that the setScreen() method from Game is not swapping the screens because after I set the screen my game still renders only what is in the render method from the game class and not what is in the render method of screen class. This is the code:
If a run this code I only get a red screen even though I change screens when the user touches(clicks) 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
.
}
You aren't using the game class properly. You shouldn't do any rendering there, that's the screens' task.
You should check out the libgdx screen and game classes wiki page. The usage should be somewhere like this:
public class MyGame extends Game {
@Override
public void create() {
setScreen(new RedScreen(this));
}
}
and have a RedScreen like this:
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 a GreenScreen like this:
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
}
found the problem the solution is not to use the render method from the game class in fact you actually HAVE TO DELETE IT from the class believe me you have to do all the render in the other screen classes.
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