Java 在 Libgdx 中处理屏幕的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19392250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:45:44  来源:igfitidea点击:

Proper way to dispose screens in Libgdx

javaandroidlibgdxdispose

提问by Chris

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?

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

采纳答案by noone

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.

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

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

这对Screens他们自己也是有效的。切换时,Screens它们不会自动处理,但您需要自己处理(在调用 之前Game.setScreen())。

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

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

BUT this does not explain your behaviour about invisible buttons from the last Screen. I suppose you use a Stageand 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 Stageof 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.

但这并不能解释您上次关于隐形按钮的行为Screen。我想你使用 aStage并使用了Gdx.input.setInputProcessor(stage);. 当您更改屏幕此设置不会改变,你必须输入处理器设为Stage您的电流Screen,或任何用于处理在当前的输入Screen。这样“旧”阶段将不再捕获任何输入。

回答by TypingTurtle

I can confirm that this issue is not passing the inpur processor a new stage. this will result in "ghost" buttons as described.

我可以确认这个问题并没有让 inpur 处理器进入一个新阶段。这将导致如上所述的“幽灵”按钮。

回答by Khalid Bhyan

Unfortunately LibGDX API documentationsays

不幸的是LibGDX API 文档

Note that dispose() is not called automatically.

请注意,不会自动调用 dispose()。

So what I do is disposing all disposables (such as Stage, Skin, Texture... etc) inside the hide()method in the Screen because hide()is called automatically and it works very well!

所以我所做的是在 Screen的方法中处理所有一次性物品(例如Stage, Skin, Texture... 等)hide(),因为hide()它会自动调用并且效果很好!

example:

例子:

public class GameScreen implements Screen {
...
    @Override
    public void hide() {
        mainStage.dispose();
        playGroundStage.dispose();
        controller.dispose();
        labelActor.dispose();
    }
...
}