问题描述
您好,我正在开发一款游戏,我想知道如何处理资源,因为我遇到了内存问题.
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 屏幕的正确位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)