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
Proper way to dispose screens in Libgdx
提问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 Screens
themselves. When switching Screens
they 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 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
并检查它是否必须处理。如果有一个Dispose方法,调用它dispose()
的Screen
。
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.
但这并不能解释您上次关于隐形按钮的行为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();
}
...
}