简体   繁体   English

LibGDX游戏暂停状态:动画闪烁/错误

[英]LibGDX Game Pause State: Animation flicker/bug

Im trying to implement a pause option for my following game. 我正在尝试为我的后续游戏实现暂停选项。 By pressing the back button my game should go to the updatePause() method. 通过按下后退按钮,我的游戏应该转到updatePause()方法。 But instead it renders the latest 2-3 frames repeatedly. 但是,它会重复渲染最新的2-3帧。 Making it look like it's "flickering". 使它看起来像“闪烁”。

By trying to prevent this I've added (in the comments) non-continously rendering which prevented the flickering. 通过尝试防止这种情况,我在注释中添加了非连续渲染,从而防止了闪烁。 But then Im facing the problem that pressing the back-button more than once will render another frame. 但是后来我面临一个问题,即多次按下后退按钮会渲染另一帧。 (one of those flickering frames). (那些闪烁的帧之一)。 This is my code structure in short: 简而言之,这是我的代码结构:

public class GameScreen implements Screen {
    private static final int GAME_RUNNING = 0;
    private static final int GAME_PAUSED = 1;
    private static final int GAME_OVER = 2;
    private static final int GAME_LEVEL_END = 3;
    private int gameState;

    private GdxGame game;

    //create()
    public GameScreen(final GdxGame game) {
        //create all the stuff
    }

    @Override
    public void render(float deltaTime) {
        switch (gameState) {
            case GAME_RUNNING:
                updateRunning(deltaTime);
                break;
            case GAME_PAUSED:
                updatePause(deltaTime);
                break;
            case GAME_OVER:
                updateGameOver();
                break;
        }
    }

    public void updateRunning(float deltaTime) {
        //draw section
        if (gameState == GAME_RUNNING) {
            game.batch.begin();
            //Draw some stuff
            game.batch.end();

            //move section
            handleInput();
        }
    }

    public void updatePause(float deltaTime) {
        //Gdx.graphics.setContinuousRendering(false);
        //resume
        if (Gdx.input.isTouched()) {
            gameState = GAME_RUNNING;
            //Gdx.graphics.setContinuousRendering(true);
            return;
        }
    }

    private void handleInput() {
        //catch back button
        Gdx.input.setCatchBackKey(true);
        if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)) {
            gameState = GAME_PAUSED;
            return;
        }
    }
}

Any tips to prevent the flickering? 有什么提示可以防止闪烁?

The render() method is supposed to draw something to the screen. render()方法应该在屏幕上绘制一些东西。 If it doesn't, then the contents of the back buffer will be undefined (so may contain a previous frame) and you get this flicker. 如果不是这样,则后缓冲区的内容将是不确定的(因此可能包含前一帧),并且会出现此闪烁。

Disabling continuous rendering sounds like a good approach. 禁用连续渲染听起来很不错。 Alternatively, continue drawing even if the game is paused. 或者,即使游戏暂停,也继续绘图。

As Thomas points out in his answer. 正如托马斯在回答中指出的那样。 It should be drawing every frame. 它应该绘制每帧。

I haven't see you draw anything on screen yet, so backbuffer might just contain junk information. 我还没有看到您在屏幕上画任何东西,因此后缓冲区可能只包含垃圾信息。 One way to solve this is to always clear the screen at the top of render() . 解决此问题的一种方法是始终清除render()顶部的屏幕。 This means when you switch immediately to another game state, it will make sure that screen is clear and no flickering result as you experienced. 这意味着当您立即切换到另一个游戏状态时,它将确保屏幕清晰,并且不会像您所经历的那样闪烁。

public void render(float deltaTime) {
    // clear screen
    Gdx.gl.glClearColor(0f, 0f, 0f, 1.0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    ...
}

屏幕上的所有对象还是仅使用动画的对象都会出现闪烁现象?

As Thomas said: continue drawing is a way to fix it. 正如托马斯所说:继续绘制是一种修复方法。 So I drew the latest frame and disabled continous rendering after that. 因此,我绘制了最新的帧,然后禁用了连续渲染。 The result is having no bug at all and giving the CPU a rest. 结果是根本没有错误,并给CPU带来了休息。

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

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