libgdx leave screen and screen lifecycle

前提是你 提交于 2021-02-07 08:42:01

问题


The following command:

game.setScreen(new GameScreen());

launches you into a new screen, similar to Android's startActivity().

But then how do you leave the screen and return the screen that called you (similar to Android's finish())?

Plus, is there a graphic showing the screen lifecycle for LibGDX similar to Android?


回答1:


The screen lifecycle is actually pretty much the same like Android's lifecycle, because that's what they had to cover when designing libgdx. Basically the Android lifecycle callback events are just forwarded to LibGDX's ApplicationListener, which in turn forwards it to your Game, which in turn forwards it to your Screen.

The lifecycle usually looks like this (using Screen terminology):

           __________________________________
           |         ____       ____        |
           V         V   |      V  |        |
show --> resume --> resize <-- render --> pause --> hide --> dispose
           |          |          ^          ^
           |__________|__________|__________|

You can see that show and hide are usually only called once. show() will be called at the beginning, when your Screen is set as the current one, hide() will be called, when you change the screen. Note that dispose() is not alled automatically, so you should make sure to call it when switching the screen, or call it in your hide() method.

resume() and pause() can be called multiple times, but at least once. Switching to another app or the homescreen will cause one more pause -> resume cycle.

render() and resize() are usually called a lot, but not necessary in any particular order. Resizing the window on desktop can cause many calls to resize() in a row, without any render() call in between. But of course resize() could also be skipped completely.

If you want to switch back to a screen which was already visible before, then you need to give the second screen a reference to the first one, so it can be set as the current screen again. But that would also cause a whole lifecycle from the beginning.

Another option would be to keep the second screen as a property of the first screen and "emulate" the screen switch yourself, by calling screen2.show(); screen2.resume(); yourself, and then forward all events to the second screen in your first one.



来源:https://stackoverflow.com/questions/21062928/libgdx-leave-screen-and-screen-lifecycle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!