Switching between screens Libgdx

寵の児 提交于 2019-11-30 05:34:48

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!