首页 文章

Libgdx屏幕混乱

提问于
浏览
-1

你好,这是我第一次在这里发帖,对编码比较新 . 我一直在玩java和libgdx一两个月,我对屏幕上的一件事感到困惑 .

如果我有一个GameScreen类在玩游戏时处于活动状态,然后想要暂时切换到另一个屏幕进行库存或暂停屏幕或某事,如果我让游戏切换回游戏屏幕,那么似乎一切都重置并且是新的比赛开始 . 什么是确保屏幕重新加载的正确方法是什么?

1 回答

  • 0

    如果您希望在后台保持屏幕处于活动状态(如您所述),则需要确保将游戏初始化代码分开,以便仅在启动新级别时调用它 . 有一百万种方法可以做到这一点 . 这是一个例子:

    public class MyGameScreen {
    
        public MyGameScreen (){
            //load textures and sounds here if you aren't managing them somewhere else.
    
            startNewGameRound();
        }
    
        @Override
        public void show (){
            //this is only for stuff you need to reset every time the screen comes back. 
            //Generally, you would undo stuff that you did in `hide()`.
        }
    
        public void startNewGameRound (){
            //Reset all your variables and reinitialize state for a new game round.
            //This is called in the constructor for first time setup, and then it 
            //can also be called by a button anywhere that starts a new round.
            //But do not load textures and sounds here. Do that in the constructor
            //so it only happens once.
        }
    
    }
    

    然后在您的Game类中,您还需要确保保留游戏屏幕实例并重复使用它 .

    不要这样做:

    setScreen(new MyGameScreen());
    

    改为:

    //member variable:
    private MyGameScreen myGameScreen;
    
    //In some method:
    if (myGameScreen == null)
        myGameScreen = new MyGameScreen();
    setScreen(myGameScreen);
    

相关问题