繁体   English   中英

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

[英]Changing to end game screen in libgdx

当我的游戏结束时,我试图将屏幕更改为最终游戏屏幕,但没有这样做,它只是在闪烁当前游戏屏幕。

我相信它正在清理舞台,但随后又重新绘制它而不移动到下一个屏幕? 好像它仍在循环渲染调用。

您能否建议如何正确切换屏幕。

   @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();
        }
    }
}

好的,现在可以使用render方法的完整代码了。 我将输入处理器的设置移至show()方法。

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

结束游戏画面

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);
}

这很可能是由于最终游戏屏幕缺少屏幕清除功能所致。 根据我的经验,这会导致行为不确定,可以在台式机和某些手机上正常工作,而在其他手机上却会发黄发白。 您仍会看到前一个屏幕,因为您没有通过清除将其从设备的硬件屏幕中删除。

因此,只需像在其他类中一样添加glClear行即可。

暂无
暂无

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

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