How to restart screen in LibGDX using Init() methods?

后端 未结 1 1849
抹茶落季
抹茶落季 2021-01-27 06:56

I created a simple game in LibGDX that has multiple screens. I want to restart a certain screen after touching a restart button but I\'m not sure how to do that. I made some res

相关标签:
1条回答
  • 2021-01-27 07:01

    show() is called automatically when the screen is first shown.

    The init() method is one you create yourself. You can call it whatever you want. You should not load all the assets in init() just set everything up, score = 0, player.setPosition(0,0) etc.

    So when you first call init() you set all variables. When you call init() again you set them again. (reset)

    Efter edit of question:

    In the methods called from GameInit() you load files that are already loaded and add actors to stages where those actors are already there and therefore now overlapping. You also add actions and create objects already created.

    All GameInit() should do is set values for those already created objects. So basically:

    private void GameInit(){
          AntAnimate.get(1).setPosition(x,y);
          AntAnimate.get(2).setPosition(x,y);
          AntAnimate.get(3).setPosition(x,y);
          levelOneImage.setPosition(0,0);
          scoreInt = 0;
          coinInt = 0;
    }
    

    Loading and creation of objects should not be done more than once. Try just adding my GameInit() method, call it GameReset() and call it from your reset button.

    0 讨论(0)
提交回复
热议问题