Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

java - LibGDX persist actors while switching screens?

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 the background 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.

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...