1
\$\begingroup\$

I made the game in libgdx and i want the splash screen in the starting but its show splash screen but not move to the game screen. I don't know what was the problem even the run time error also not occur.

GameScreen. java

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.gamestudio24.martianrun.stages.GameStage;

public class GameScreen implements Screen  {

    private GameStage stage;

    public GameScreen() {
        stage = new GameStage();
    }

    @Override
    public void render(float delta) {
        //Clear the screen
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //Update the stage
        stage.draw();
        stage.act(delta);
    }

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

    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

}

GdxSplashScreenGame. java

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Timer;

public class GdxSplashScreenGame extends Game {

    private static long SPLASH_MINIMUM_MILLIS = 2000L;

    public GdxSplashScreenGame() { super(); }

    @Override
    public void create () {
        setScreen(new SplashScreen());

        final long splash_start_time = System.currentTimeMillis();
        new Thread(new Runnable() {
            @Override
            public void run() {

                Gdx.app.postRunnable(new Runnable() {
                    @Override
                    public void run() {
                        long splash_elapsed_time = System.currentTimeMillis() - splash_start_time;
                        if (splash_elapsed_time < GdxSplashScreenGame.SPLASH_MINIMUM_MILLIS) {
                            Timer.schedule(
                                    new Timer.Task() {
                                        @Override
                                        public void run() {
                                            GdxSplashScreenGame.this.setScreen(new SplashScreen());
                                        }
                                    }, (float)(GdxSplashScreenGame.SPLASH_MINIMUM_MILLIS - splash_elapsed_time) / 1000f);
                        } else {
                            GdxSplashScreenGame.this.setScreen(new SplashScreen());
                        }
                    }
                });
            }
        }).start();
    }

    @Override
    public void dispose() {
        // DISPOSE ALL RESOURCES
        getScreen().dispose();
        Gdx.app.exit();
    }
}

SplashScreen. java

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class SplashScreen extends GameScreen {
    private SpriteBatch batch;
    private Texture ttrSplash;

    public SplashScreen() {
        super();
        batch = new SpriteBatch();
        ttrSplash = new Texture("assets/splash.png");
    }

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

        batch.begin();
        batch.draw(ttrSplash, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.end();
    }

    @Override
    public void hide() { }

    @Override
    public void pause() { }

    @Override
    public void resume() { }

    @Override
    public void show() { }

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

    @Override
    public void dispose() {
        ttrSplash.dispose();
        batch.dispose();
    }
}

MartianRun. java

import com.badlogic.gdx.Game;
import com.gamestudio24.martianrun.screens.GameScreen;
import com.gamestudio24.martianrun.screens.SplashScreen;
import com.gamestudio24.martianrun.utils.AssetsManager;
import com.gamestudio24.martianrun.utils.AudioUtils;
import com.gamestudio24.martianrun.utils.GameEventListener;
import com.gamestudio24.martianrun.utils.GameManager;

public class MartianRun extends Game {

    public MartianRun(GameEventListener listener) {
        GameManager.getInstance().setGameEventListener(listener);
    }



    @Override
    public void create() {
        AssetsManager.loadAssets();
        setScreen(new SplashScreen());
        setScreen(new GameScreen());

    }

    @Override
    public void dispose() {
        super.dispose();
        AudioUtils.dispose();
        AssetsManager.dispose();
    }
}
\$\endgroup\$
1
\$\begingroup\$

I think you're over complicating things by using threads and timers. You can use your GdxSplashScreenGame as the main parent class which controls which screen should be shown. Then when you create a screen your simply pass the parent (GdxSplashScreenGame) to the screens and let the screens control when they are finished.

If you change your main game class to include a static var for each screen and a method to change screens like this:

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;

public class GdxSplashScreenGame extends Game {

    // one for each possible screen
    public static final int SPLASH_SCREEN = 0;
    public static final int GAME_SCREEN = 1;
    public static final int GAME_OVER_SCREEN = 3;

    public GdxSplashScreenGame() { super(); }

    @Override
    public void create () {
        changeScreen(SPLASH_SCREEN);
    }

    @Override
    public void dispose() {
        // DISPOSE ALL RESOURCES
        getScreen().dispose();
        Gdx.app.exit();
    }

    public void changeScreen(int screen){
        if(screen == SPLASH_SCREEN){
            this.setScreen(new SplashScreen(this));
        }else if(screen == GAME_SCREEN){
            this.setScreen(new GameScreen(this));
        }
    }
} 

Then when you pass the this class to your screens you can simply call

parent.changeScreen(GdxSplashScreenGame.GAME_SCREEN);

Now instead of using a thread and a timer to change the screen after a certain time. You can use the built in delta time supplied in the render method to use as a countdown timer like this:

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class SplashScreen implements Screen {
    private SpriteBatch batch;
    private Texture ttrSplash;
    private GdxSplashScreenGame parent;

    private float timeToShowSplashScreen = 2f; // 2 seconds 

    // pass the parent game to this screen so this screen can tell the parent its finished
    // and tell it to load the next screen
    public SplashScreen(GdxSplashScreenGame p) {
        super();
        parent = p;
        batch = new SpriteBatch();
        ttrSplash = new Texture("assets/splash.png");
    }

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

        batch.begin();
        batch.draw(ttrSplash, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.end();

        timeToShowSplashScreen -= delta; // remove delta from time
        if(timeToShowSplashScreen <= 0){ // 2 seconds are up
            // tell parent to change screen
            parent.changeScreen(GdxSplashScreenGame.GAME_SCREEN);
        }
    }

    @Override
    public void hide() { }

    @Override
    public void pause() { }

    @Override
    public void resume() { }

    @Override
    public void show() { }

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

    @Override
    public void dispose() {
        ttrSplash.dispose();
        batch.dispose();
    }
}
|improve this answer|||||
\$\endgroup\$

Your Answer

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