Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...