在 Libgdx 中处理屏幕的正确方法 [英] Proper way to dispose screens in Libgdx

查看:25
本文介绍了在 Libgdx 中处理屏幕的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Libgdx 中完全处理屏幕的正确方法是什么?当前,如果我单击上一个屏幕上的按钮位置,则该按钮仍会执行如果我在该屏幕上时会执行的操作.我应该 .dispose() - 在 dispose() 方法中尽我所能吗?还是有更简单的方法来处理屏幕上的所有内容?

What is the proper way of completely disposing of a screen in Libgdx? Currently If I click where a button was on my previous screen the button still does what it would of done if I were on that screen. Should I be .dispose()-ing everything I can in the dispose() method? or is there a simpler way to dispose of everything on screen?

推荐答案

不幸的是,没有更简单的方法.这些类不共享任何类型的通用Disposable"接口或类似的接口来自动完成.所有具有 dispose() 方法的东西都需要在不再需要时手动处理.

Unfortunately there is no easier way. These classes do not share any kind of common "Disposable" interface, or anything like that, to do it automatically. Everything that has a dispose() method needs to be disposed manually when it's not needed anymore.

这对 Screens 本身也有效.切换 Screen 时,它们不会自动处理,但您需要自己执行此操作(在调用 Game.setScreen() 之前).

This is also valid for the Screens themselves. When switching Screens they do not get disposed automatically, but you need to do that yourself (before calling Game.setScreen()).

另一方面,这没什么大不了的.只需查看您的 Screen 中的所有内容并检查是否必须处理它.如果有dispose方法,在Screendispose()中调用.

On the other hand, this is not a big deal. Just look through everything in your Screen and check whether it has to be disposed or not. If there is a dispose method, call it in dispose() of the Screen.

但这并不能解释您对上一个 Screen 中不可见按钮的行为.我想你使用了 Stage 并使用了 Gdx.input.setInputProcessor(stage);.当您更改屏幕时,此设置不会更改,您必须将输入处理器设置为当前 ScreenStage,或者设置为处理您的输入的任何内容当前屏幕.这样,旧"阶段将不再捕获任何输入.

BUT this does not explain your behaviour about invisible buttons from the last Screen. I suppose you use a Stage and used Gdx.input.setInputProcessor(stage);. This setting will not be changed when you change the screen and you have to set the input processor to the Stage of your current Screen, or to whatever that handles the input in your current Screen. That way the "old" stage will not catch any inputs anymore.

这篇关于在 Libgdx 中处理屏幕的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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