简体   繁体   English

更改为libgdx中的结束游戏屏幕

[英]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. 好的,现在可以使用render方法的完整代码了。 I moved the setting of the input processor to the show() method. 我将输入处理器的设置移至show()方法。

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. 这个问题可能是因为game.setScreen的方法位于render方法中,但是我不确定如何在show方法中实现此方法,因为当演员到达某个位置时会发生这种情况,所以我听不到进行输入触摸或其他操作。

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. 因此,只需像在其他类中一样添加glClear行即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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