什么是配置 libgdx 屏幕的正确位置

2022-01-12 00:00:00 java libgdx

您好,我正在开发一款游戏,我想知道如何处理资源,因为我遇到了内存问题.

Hello I am working out a game and I wonder how to dispose resources as I am experiencing memory problems.

我有这样的事情:

public SplashScreen implements Screen {
    @Override
    public void render(float delta) {
    }
    @Override
    public void dispose() {
        if (batch != null)
            batch.dispose();
        batch = null;
    }
}

public MapScreen implements Screen {
    @Override
    public void render(float delta) {
    }
    @Override
    public void show() {
        splashScreenInstance.dispose();
    }
    @Override
    public void dispose() {
        if (mesh != null)
        mesh.dispose();
        mesh = null;
    }
}

一旦调用 MapScreen 的 show 方法,我就会处理启动画面.以前我将屏幕设置为 MapScree.仍然调用了 splashScreenInstance 的渲染方法,并且我收到了空指针异常.为什么会这样?

And I am disposing the splash screen as soon as the show method of MapScreen is called. Previously I'd settled the screen to the MapScree. Still the render method of the splashScreenInstance is called and I'd received null pointer exceptions. Why this is so?

我希望一旦我设置了另一个屏幕,前一个屏幕将不再呈现.这似乎并非如此.我尝试在使用游戏实例设置屏幕后立即进行处理,在我要处理的屏幕上调用 hide 方法之后,最后在下一个屏幕的 show 方法上进行处理.所有这些情况在渲染当前屏幕之前仍然会渲染前一个屏幕几次.

I'd expect that once I set another screen, the previous one is no longer rendered. This is not seemingly so. I'd tried disposing right after setting the screen using the game instance, right after the hide method is called on the screen I want to dispose and finally on the show method of the next screen. All of these cases still renders the previous screen a few times before rendering the current one.

我真的需要恢复内存,而且我不想每次(在渲染方法上)都测试空指针,因为这会降低性能.

I really need to recover memory and also I don't want to test each time (On the render method) for null pointers as this has performance penalties.

有什么建议吗?

谢谢

推荐答案

你在哪里调用 setScreen?由于一切都应该在渲染线程(甚至 InputListeners)中发生,您应该能够在您的第一个 Screen 中调用 setScreen,然后从渲染方法返回.Game 实例将在您的第一个 Screen 上自动调用 hide,您可以在此处调用 dispose.

Where are you calling setScreen? Since everything should be happening in the rendering thread (even InputListeners) you should be able to call setScreen in your first Screen and then return from the render method. The Game instance will automatically call hide on your first Screen which is where you can call dispose.

相关文章