Fth
Fth

Reputation: 115

Switch screens (libgdx) - Game class implementing ApplicationListener

We are working on a game with libgdx and we want to be able to switch screens. I have made a GameOverScreen, which implements Screen:

public class GameOverScreen implements Screen {
}

My problem is that i don't know how to set the screen in my main class. Most of the examples i have looked at shows a main class which extends Game (com.badlogic.gdx.Game). But our main class implements ApplicationListener and doesn't extends Game:

public class Game implements ApplicationListener {
}

Therefore i can't use the setScreen method from the Game class. So how can i change the screen in my main class Game?

Upvotes: 0

Views: 1568

Answers (3)

Covering Stark
Covering Stark

Reputation: 14

Mainly you need 3 classes

  • ScreenManager Class
  • Abstract Screen Class (Optional)
  • ScreenEnum (Enum)

Play screen and main menu screen will be extended from Abstract screen. Screenmanager switches the screen according to screen code from screenenum

****Screen Enum Class****

public enum ScreenEnum {
    MAIN_MENU {

        public AbstractScreen getScreen(Object... params) {
            return new MainMenuScreen();
        }
    },
    PLAY {

        public AbstractScreen getScreen(Object... params) {
            return new PlayScreen();
        }
    };
    public abstract AbstractScreen getScreen(Object... params);
}

ScreenManager Class

public class ScreenManager {

    private static ScreenManager instance;

    private Game game;

    private ScreenManager() {
        super();
    }

    public static ScreenManager getInstance() {
        if (instance == null) {
            instance = new ScreenManager();
        }
        return instance;
    }
    public void initialize(Game game) {
        this.game = game;
    }
    public void showScreen(ScreenEnum screenEnum, Object... params) {
        Screen currentScreen = game.getScreen();

        AbstractScreen newScreen = screenEnum.getScreen(params);

        newScreen.buildStage();
        game.setScreen(newScreen);

        // Dispose previous screen
        if (currentScreen != null) {
            currentScreen.dispose();
        }
    }
}

AbstarctScreen Class (Optional class)

public abstract class AbstractScreen implements Screen {

    protected AbstractScreen() {
        super( );
    }
    public abstract void buildStage();

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    }

    @Override
    public void show() {

    }
    @Override
    public void resize(int width, int height){
    }

    @Override public void hide() {}
    @Override public void pause() {}
    @Override public void resume() {}
}

Screen Switch

ScreenManager.getInstance().showScreen(ScreenEnum.PLAY, 0);

ScreenManager.getInstance().showScreen(ScreenEnum.MAIN_MENU, 0);

Upvotes: 0

Terran
Terran

Reputation: 1153

com.badlogic.gdx.Game does nothing else but also implement ApplicationListener. There are some simple options:

  • So you could just extend com.badlogic.gdx.Game instead of implementing ApplicationListener

  • Do the same as com.badlogic.gdx.Game does. For example:

    public void setScreen (Screen screen) {
        if (this.screen != null) this.screen.hide();
        this.screen = screen;
        if (this.screen != null) {
            this.screen.show();
            this.screen.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        }
    }
    
  • Use Composition / Adapter: Create a field of com.badlogic.gdx.Game in your ApplicationListener implementing class and delegate the methods you implement.

Upvotes: 1

AAryan
AAryan

Reputation: 20140

  • First of all Game is predefined class so use different class name instead of Game for your own implementation.

  • com.badlogic.gdx.Game is nothing more than ApplicationListener, it has only a reference of Screen so having setScreen() method.

  • Extend your Main(origin) class with Game instead of writing own implementation because you need Screen transition in your game.

Some Rules of SE :

  • Never Write the Same Code Twice.

  • Don't use hand to break a brick if already you've hammer.

Upvotes: 1

Related Questions