What#39;s the right place to dispose a libgdx Screen(什么是配置 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
.
这篇关于什么是配置 libgdx 屏幕的正确位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是配置 libgdx 屏幕的正确位置
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01