libgdx 离开屏幕和屏幕生命周期

libgdx leave screen and screen lifecycle(libgdx 离开屏幕和屏幕生命周期)

本文介绍了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())?

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

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
           |          |          ^          ^
           |__________|__________|__________|

您可以看到 show 和 hide 通常只调用一次.show() 将在开始时调用,当您的 Screen 设置为当前屏幕时,将调用 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 离开屏幕和屏幕生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:libgdx 离开屏幕和屏幕生命周期

基础教程推荐