libgdx离开屏幕和屏幕生命周期 [英] libgdx leave screen and screen lifecycle

查看:62
本文介绍了libgdx离开屏幕和屏幕生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下命令:

game.setScreen(new GameScreen());

将您启动到一个新屏幕,类似于Android的 startActivity().

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

但是您如何离开屏幕并返回调用您的屏幕(类似于Android的 finish())?

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

此外,是否有图形显示 LibGDX 的屏幕生命周期,类似于Android?

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

推荐答案

屏幕生命周期实际上与Android生命周期几乎相同,因为这是设计libgdx时必须涵盖的内容.基本上,Android生命周期回调事件只是转发到LibGDX的 ApplicationListener ,后者又将其转发到您的 Game ,后者又将其转发到您的 Screen .

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.

生命周期通常如下所示(使用 Screen 术语):

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

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

您可以看到,显示和隐藏通常仅被调用一次.当您将 Screen 设置为当前屏幕时,将在开始时调用 show(),当您将 hide()设置为当前屏幕时更换屏幕.请注意, dispose()不会自动绑定,因此您应确保在切换屏幕时调用它,或在您的 hide()方法中调用它.

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() pause(),但至少可以调用一次.切换到另一个应用程序或主屏幕将导致一个 pause->恢复周期.

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() resize()通常被称为很多,但是没有必要按任何特定顺序进行.在桌面上调整窗口大小可能会导致连续多次调用 resize(),而中间没有任何 render()调用.但是当然 resize()也可以完全跳过.

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.

另一种选择是通过调用 screen2.show();将第二个屏幕保留为第一个屏幕的属性,并自己模拟"屏幕切换.screen2.resume(); 自己,然后将所有事件转发到第一个屏幕的第二个屏幕.

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.

这篇关于libgdx离开屏幕和屏幕生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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