Question

I have two clases, one for main methods and the other for splash. However, switching between these two doesn't work. I wanted to draw an image in the Splash class, but it turned black instantly. And when I moved the code from the splash to main class, the image appeared.

Main class:

public class Main extends Game {
public static int WIDTH, HEIGHT;

public void create () {
    WIDTH = Gdx.graphics.getWidth();
    HEIGHT = Gdx.graphics.getHeight();
    setScreen(new Splash());
}

public void render () { }
public void dispose() { super.dispose(); }
public void pause() { super.pause(); }
public void resize(int width, int height) { super.resize(width, height); }
public void resume() { }
}

Splash class:

public class Splash implements Screen {

private Sprite splash;
private SpriteBatch sb;

public void show() {
    sb = new SpriteBatch();
    Texture splashTexture = new Texture(Gdx.files.internal("res/img/splash.png"));
    splash = new Sprite(splashTexture);
    splash.setSize(Main.WIDTH, Main.HEIGHT);
    Gdx.gl.glClearColor(0, 0, 0, 1);
}

public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    sb.begin();
        splash.draw(sb);
    sb.end();
}

public void resize(int width, int height) { }
public void resume() { }
public void dispose() { }
public void hide() { }
public void pause() { }
}

Any idea, what could cause the problem of not rendering the image in Splash class?

UPDATE 1: I have discovered, that the render() method inside the Splash class doesn't even get called (but the show() method does)

Was it helpful?

Solution

You should call the render method from your game class. If you dont call it noone will do that for you. You shouldn't override the render method of the game class which you do and you do it empty so it wont call anything for you.

Lets take a look into the Game class. It does implement all methods from the ApplicationListener interface (well not the create()). So there is no need for you to override anything of it. Simply delete your:

public void render () { }
public void dispose() { super.dispose(); }
public void pause() { super.pause(); }
public void resize(int width, int height) { super.resize(width, height); }
public void resume() { }

and it should work fine. Even though these are useless methods. They do nothing than calling the super class so why you write those. If you have not written that it automatically calls the super methods.

But okay if you want to handle the stuff yourself like calling dispose on a screen or calling the init before you set a new screen you need to write your own "game" class which implements the ApplicationListener interface.

To give you an idea on how to do this ill post a small example which i use for some tests:

public class MyGameClass implements ApplicationListener {

    private Screen curScreen; // the current screen

    @Override
    public void create(){
        Gdx.graphics.getWidth();
        Gdx.graphics.getHeight();

        Intro temp = new Intro(this);//just an example of the first screen to load up
        setScreen(temp);
    }

    public void setScreen(Screen s) {
        if (curScreen != null) {
            curScreen.hide();
            curScreen.dispose();
        }
        curScreen = s;
        curScreen.show();
        curScreen.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        s.init();
    }

    @Override
    public void dispose() {
        curScreen.dispose();
    }

    @Override
    public void render() {
        curScreen.render(Gdx.graphics.getDeltaTime()); //call the rendermethod with the delta time as parameter
    }

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

    @Override
    public void pause() {
        curScreen.pause();
    }

    @Override
    public void resume() {
        curScreen.resume();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top