什么是处理libgdx屏幕的正确位置 [英] What's the right place to dispose a libgdx Screen

查看:126
本文介绍了什么是处理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;
    }
}

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

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),你应该能够在你的第一个屏幕中调用 setScreen 然后从render方法返回。 游戏实例将在您的第一个屏幕上自动调用隐藏您可以在此处拨打 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.

这篇关于什么是处理libgdx屏幕的正确位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆