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

£可爱£侵袭症+ 提交于 2019-12-02 07:50:26

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!