Libgdx render() 方法在上一次调用完成之前被调用两次

问题描述 投票:0回答:1
public class SplashScreen extends AbstractScreen {

    private float countDown;
    private final GameManager gameManager;
    boolean updating = false;

    public SplashScreen(Main main) {
        super(main);
        gameManager = GameManager.get();

        Label loading = new Label("Loading", new Label.LabelStyle(new BitmapFont(), Color.BLACK));

        Table table = new Table();
        table.setFillParent(true);
        table.center();
        table.add(loading).expand();

        stage.addActor(table);

        countDown = 0.5f;
    }

    @Override
    public void show() {
        super.show();
        main.loadManager();
    }

    @Override
    public void render(float delta) {
        super.render(delta);
        
        if (updating) throw new IllegalStateException();
        
        updating = true;
        
        countDown -= delta;
        if (countDown < 0.0f && gameManager.assetManager.update()) {
            main.onLoadManager();
            ScreenManager.get().showScreen(ScreenManager.ScreenEnum.MAIN_MENU);
        }
        
        updating = false;
    }

}

此代码会导致错误,因为 render() 几乎同时被调用两次。

Exception in thread "main" java.lang.IllegalStateException
    at avseygames.convey.screens.SplashScreen.render(SplashScreen.java:51)
    at com.badlogic.gdx.Game.render(Game.java:48)
    at avseygames.convey.Main.render(Main.java:41)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Graphics.renderWindow(Lwjgl3Graphics.java:103)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Graphics.access$000(Lwjgl3Graphics.java:45)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Graphics$1.invoke(Lwjgl3Graphics.java:80)
    at org.lwjgl.glfw.GLFWFramebufferSizeCallbackI.callback(GLFWFramebufferSizeCallbackI.java:44)
    at org.lwjgl.system.JNI.invokePPV(Native Method)
    at org.lwjgl.glfw.GLFW.glfwSetWindowMonitor(GLFW.java:3017)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Graphics.setFullscreenMode(Lwjgl3Graphics.java:446)
    at avseygames.convey.manage.GameManager.setScreenSize(GameManager.java:78)
    at avseygames.convey.Main.onLoadManager(Main.java:28)
    at avseygames.convey.screens.SplashScreen.render(SplashScreen.java:56)
    at com.badlogic.gdx.Game.render(Game.java:48)
    at avseygames.convey.Main.render(Main.java:41)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Window.update(Lwjgl3Window.java:387)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.loop(Lwjgl3Application.java:193)
    at com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application.<init>(Lwjgl3Application.java:167)
    at avseygames.convey.DesktopLauncher.main(DesktopLauncher.java:15)

FAILURE: Build failed with an exception.

这只是一个错误的演示,如果您注释掉“更新”,则新屏幕的创建会完成两次。 如何防止双重渲染?

我尝试删除 AssetManager,但这显然不是重点。无论如何,新屏幕都会创建两次。

java libgdx game-development lwjgl
1个回答
0
投票

作为“render”方法中的临时解决方案

if (updating) throw new IllegalStateException();
我把它替换为
if (updating) return;

© www.soinside.com 2019 - 2024. All rights reserved.