LibGDX在切换屏幕时是否坚持演员

LibGDX在切换屏幕时是否坚持演员

本文介绍了LibGDX在切换屏幕时是否坚持演员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用LibGDX制作游戏,并且我有几个使用scene2d ui的菜单屏幕.我有一个MenuBaseScreen超类,它设置每个屏幕所需的默认actor.然后我有一个MainMenu,Options等屏幕,每个屏幕都从MenuBaseScreen继承.在每个屏幕中,视差背景一直从右向左移动.此视差背景在MenuBaseScreen中实例化.

I'm making a game in LibGDX and I have a couple of menu screens using the scene2d ui.I have one MenuBaseScreen super class that sets up the default actors required for each screen. Then i have a MainMenu, Options etc screens that each inherit from MenuBaseScreen.In each of the screens there is a parallax background that keeps going from right to left.This parallax background gets instantiated in the MenuBaseScreen.

现在的问题是,每次我切换屏幕时都会有一个快速的滞后,因为背景必须重新实例化,并再次添加到舞台上,因为每个子类都在发生所有这些情况的地方调用了超级构造函数.

Now the problem with this is that each time I switch screen there is a quick lag because thebackground have to get re-instantiated and added to the stage again since each subclass calls the super constructor where all of this happens.

所以我想知道是否有办法在切换屏幕时保留此图像及其动画?

So I was wondering if there is a way to persist this image and its animation while switching screens?

当然,我可以替换背景以外的所有演员,并且只有一个屏幕而不是多个屏幕.但是我认为这种方法有点笨拙,并且容易出错.

Of course I could just replace all the actors except my background and just have one screen instead of many. But I think that approach is kind of clunky and makes for error prone code.

谢谢.

推荐答案

为什么不将需要的actor/组件作为在多个屏幕中使用的static成为MenuBaseScreen的一部分.另外,您可能希望将libGDX中已有的Screen接口与ApplicationListener类的Game类一起使用.更多信息此处.

Why not make the actors/components needed part of the MenuBaseScreen as static that are used in multiple screens. Also you might want to look at using the Screen interface already in libGDX with the Game class for your ApplicationListener class. More info here.

这篇关于LibGDX在切换屏幕时是否坚持演员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:13