代码之家  ›  专栏  ›  技术社区  ›  Manh Khôi Duong

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

  •  3
  • Manh Khôi Duong  · 技术社区  · 9 年前

    我试图为我的下一个游戏实现一个暂停选项。通过按下后退按钮,我的游戏应该转到updatePause()方法。但它会重复渲染最近的2-3帧。让它看起来像“闪烁”。

    为了防止这种情况,我(在评论中)添加了非连续渲染,以防止闪烁。但是我面临的问题是,多次按下后退按钮将渲染另一帧。(闪烁的帧之一)。简而言之,这是我的代码结构:

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

    3 回复  |  直到 9 年前
        1
  •  4
  •   Thomas    9 年前

    这个 render()

    禁用连续渲染听起来是个不错的方法。或者,即使游戏暂停,也可以继续绘制。

        2
  •  1
  •   haxpor    9 年前

    正如托马斯在回答中指出的那样。它应该绘制每个帧。

    render() . 这意味着,当你立即切换到另一个游戏状态时,它将确保屏幕清晰,不会出现你体验到的闪烁结果。

    public void render(float deltaTime) {
        // clear screen
        Gdx.gl.glClearColor(0f, 0f, 0f, 1.0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        ...
    }
    
        3
  •  0
  •   Manh Khôi Duong    9 年前

    正如托马斯所说:继续画画是一种解决问题的方法。所以我画了最新的一帧,然后禁用了连续渲染。结果是没有任何错误,让CPU休息。