0
\$\begingroup\$

Today I was experimenting with adding different screens with Scene2d in libgdx and while doing so I became confused how super.render() in my render() method from my starter class (MyGdxGame) called render() in my PlayScreen() class. Basically I understand you use super to call methods, fields, invoke a super class constructor etc... however I don't see how my Main class witch extends Game is a subclass/derived class of my PlayScreen class witch simply implements Screen. In short I don't see how those two classes are related so that super.render() in MyGdxGame class can call render() in my PlayScreen class.

Also I looked at the API and Screen implements ScreenAdapter and Game implements ApplicationListener but I still don't see how that connects.

Starter class:

public class MyGdxGame extends Game {
    Game game;

    @Override
    public void create() {
        game = this;
        setScreen(new MainMenu(game));
    }

    @Override
    public void render() {
        super.render();
    }

    public void dispose() {
        super.dispose();
    }

    public void pause() {
        super.pause();
    }
}

PlayScreen class:

public class PlayScreen implements Screen {
    private static Player player;
    SpriteBatch batch;
    GameControls x;
    CollisionManager collisionManager;
    ArrayList<Tree> trees;
    Iterator itr;
    Enemy enemy;
    Animations animation;
    Game game;
    MainMenu mainMenu;

    public PlayScreen(Game game, MainMenu mainMenu) {
        this.game = game;
        this.mainMenu = mainMenu;
    }
// more code ...
}
\$\endgroup\$
1
\$\begingroup\$

Your IDE of choice should have a way for you to navigate to the source for a specified class. For IntelliJ IDEA try moving your cursor to the Game class name that MyGdxGame extends and pressing CTRL + B. That should take you to the source for Game. Take a look at the override for render() that Game implements.

@Override
public void render () {
    if (screen != null) screen.render(Gdx.graphics.getDeltaTime());
}

It calls into the active screen object (set previously by setScreen(Screen)). That's how they are linked. :)

You can always reference the git repository as well, if that's easier for you in the future: https://github.com/libgdx/libgdx/tree/master/gdx/src/com/badlogic/gdx

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.