简体   繁体   中英

Changing to end game screen in libgdx

When my game finishes, i'm trying to change the screen to the end game screen, but instead of doing so, it simply flashes the current game screen.

I belive it is clearing the stage, but then drawing it again without moving to the next screen? As though its still looping the render call.

Can you advise on how to switch screen properly.

   @Override
public void render(float v) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    if (state == gamestate.PAUSED) {
        // draw pause screen
    }
    else if (state == gamestate.GAMEOVER || score <= 0) {
        game.setScreen(new endGameScreen(game, score));
        stage.clear();
        return;
    }
    else {
        player.update();

        stage.draw();
        batch.begin();
        mBtn.setPosition((cam.position.x - (Gdx.graphics.getWidth() / 2)) + 10, (cam.position.y - (Gdx.graphics.getHeight() / 2)) + 10);
        pBtn.setPosition((cam.position.x + (Gdx.graphics.getWidth() / 2)) - pauseWidth, (cam.position.y - (Gdx.graphics.getHeight() / 2)) + 10);

        batch.draw(player.getCurrentFrame(), player.getPosition().x, player.getPosition().y);

        cam.position.set(player.getPosition().x, Gdx.graphics.getHeight() / 2, 0);
        batch.setProjectionMatrix(cam.combined);
        cam.update();

        batch.end();

        if(player.getPosition().x >= finishLine.getX()) {
            System.out.println("End Game!!!");
            endGame();
        }
    }
}

Okay, so the full code for the render method is now up. I moved the setting of the input processor to the show() method.

This problem could be because the method of game.setScreen is in the render method, but i'm not sure how I could implement this in the show method, as it happens when the actor reaches a certain location, so I can't listen out for an input touch or anything.

End Game Screen

public class endGameScreen implements Screen {

int score = 0;
String scoreTxt = "";
Stage stage;
SpriteBatch batch;
BitmapFont font;
Game game;

public endGameScreen(Game game, int score){
    this.score = score;
    this.game = game;
}

@Override
public void render(float v) {
    batch.begin();
    font.setColor(1.0f, 1.0f, 1.0f, 1.0f);
    font.draw(batch, scoreTxt, 25, 100);
    batch.end();
}

@Override
public void show() {
    batch = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("font.fnt"));
    stage = new Stage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),true);
}

It's very likely caused by the lack of screen clearing in the end game screen. From my experience this results in undefined behavior, working okay on desktop and some phones while it gets crazy yellowish whiteish flashy on others. You still see the previous screen because you don't remove it from the device's hardware screen by clearing.

So just add the glClear line like in the other class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM