Switching between screens Libgdx

后端 未结 2 484
我寻月下人不归
我寻月下人不归 2020-12-28 23:16

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

相关标签:
2条回答
  • 2020-12-28 23:46

    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.

    0 讨论(0)
  • 2020-12-28 23:53

    You have an example for this and many other concepts on LibGDX.info

    0 讨论(0)
提交回复
热议问题