Hey everyone I am still working on this libgdx project and I am trying to figure out the best way to change the screens to my game screen Now, when a button is clicked I nee
This is how I always implement screen switching:
First the main class needs to extend Game (From com.badlogic.gdx.Game
) and you will need to have a new field of type Game
:
public class ConnectFourApplication extends Game{
private Game game;
Now initialize game
in the constructor:
public ConnectFourApplication(){
game = this; // Since this class extends Game
To set screen to MainScreen
, now, all you need to do is to use setScreen(new MainScreen(game));
method (passing game
so we can set screens from MainScreen
class)
You now need a new constructor for MainScreen
class, and a new field:
private Game game;
public MainScreen(Game game){
this.game = game;
Now you can use game.setScreen(new Screen(game));
to set the screen to yet another class that implements Screen
.
But now, in the main class, in the render()
method you must use super.render();
to make use everything from other screens render!
public void render() {
clearWhite();
super.render();
}
PS: Make sure that the class you are making as a screen, actually, implements Screen
.
You have an example for this and many other concepts on LibGDX.info