如何使用Init()方法重新启动LibGDX中的屏幕? [英] How to restart screen in LibGDX using Init() methods?

查看:58
本文介绍了如何使用Init()方法重新启动LibGDX中的屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在LibGDX中创建了一个具有多个屏幕的简单游戏.我想在触摸重新启动按钮后重新启动某个屏幕,但是我不确定该怎么做.我对此进行了一些研究,所有答案都导致我的资产没有加载到show()中,而是加载了一个我并不真正熟悉的init()方法中. 我想知道如何使用此init()方法重新启动屏幕.到目前为止,我将大部分初始化工作都放在了构造函数中,其中一些已在诸如restartButton()方法之类的方法中进行了初始化.我的代码中的任何其他更正或改进将不胜感激.这是我的代码段:

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 research about this and all answers lead to not loading my assets in show() but rather in an init() method that I am not really familiar with. I want to know how can I restart a screen using this init() method. As of now I put most of my initialization in the constructor and some were initialized in methods such as the restartButton() method. Any other corrections or improvements in my code will be highly appreciated. Here is a snippet of my code:

public class LevelOneScreen implements Screen {

public MainShooter app;
private Stage stage;
private Stage stageCoin;
private Stage stageScore;
private Stage stageEnemies;
private Stage stageFX;
private Stage stageButton;

public Image aImage;
public Image bImage;
public Image cImage;
public Image dImage;

public Array<AntAnimation> AntAnimate;
public Array<CrumbAnimation> CrumbAnimate;
public Array<CoinAnimation> CoinAnimate;

public Texture firstTex;
public Texture secTex;
public Texture thirdTex;
public Texture fourthTex;


 public LevelOneScreen(final MainShooter app){

 this.app = app;
 this.stage = new Stage(new StretchViewport(app.screenWidth, app.screenHeight, app.camera));
 this.stageCoin = new Stage(new StretchViewport(app.screenWidth, app.screenHeight, app.camera));
 this.stageScore = new Stage(new StretchViewport(app.screenWidth, app.screenHeight, app.camera));
this.stageEnemies = new Stage(new StretchViewport(app.screenWidth, app.screenHeight, app.camera));
this.stageFX = new Stage(new StretchViewport(app.screenWidth, app.screenHeight, app.camera));
this.stageButton = new Stage(new StretchViewport(app.screenWidth, app.screenHeight, app.camera));

AntAnimate = new Array<AntAnimation>();
CrumbAnimate = new Array<CrumbAnimation>();
CoinAnimate = new Array<CoinAnimation>();

restartButton();
levelOneBackground();
coinScore();


}


public void show() {


    inputMultiplexer = new InputMultiplexer();
    inputMultiplexer.addProcessor(stageCoin);
    inputMultiplexer.addProcessor(stageScore);
    inputMultiplexer.addProcessor(stageEnemies);
    inputMultiplexer.addProcessor(stageFX);
    inputMultiplexer.addProcessor(stageButton);

    Gdx.input.setInputProcessor(inputMultiplexer);

   }

   public void levelOneBackGround(){
   //code for the background 
   }


   public void coinScore(){
   //code for coinScore
   }

   public void restartButton(){
   firstTex = MainShooter.manager.get("button.png", Texture.class);
   aImage = new Image(firstTex);
   stageButton.addActor(aImage);
   aImage.addListener(new ActorGestureListener() {
        public void touchDown(InputEvent event, float x, float y, int pointer, int button) {

        // Creating new LevelOneScreen somehow affects the fps of my game.
        app.setScreen(new LevelOneScreen(app));


        }});
   }


//some other codes like dispose, hide, etc.

}

此编辑是对IronMonkey提供的答案的回应,该答案似乎可以解决问题,但需要其他代码.这里的问题是,每当游戏重新启动时,重新启动屏幕上的actors就会位于上一个屏幕(重新启动之前的屏幕)上actor的顶部.这是我的代码的新片段,展示了如何使用GameInit()重新启动游戏:

This edit is a response to an answer provided by IronMonkey which seems to solve the problem but needs additional code. The problem here is that whenever the game is restarted, the actors from the restarted screen is on top of the actors on the previous screen(the screen before restarting). Here is a new snippet of my code showing how to restart the game using my GameInit():

     public void GameInit(){
     levelOneBackground();
     scoreInt = 0;
     coinInt = 0;
}


public void levelOneBackground(){

        Runnable runAntAct1 = new Runnable(){
            public void run(){
                antAct1();
            }
        };
        Runnable runAntAct2= new Runnable(){
            public void run(){
                antAct2();
            }
        };
        Runnable runAntAct3 = new Runnable(){
            public void run(){
                antAct3();
            }
        };
        Runnable runCoins = new Runnable(){
            public void run(){
                coinScattered();
            }
        };
        levelOneTexture =  CoinAssets.manager.get("woodenTable.png",Texture.class);
        levelOneImage = new Image(levelOneTexture);
        levelOneImage.setSize(app.screenWidth,app.screenHeight);
        levelOneImage.setPosition(0,0);
        stage.addActor(levelOneImage);
        levelOneImage.addAction(sequence(run(runAct1),delay(2f),run(runAct2),delay(2f),run(runAct3),delay(2f),run(runCoins)));


}

public void restartButton(){
     //some code for the position, size, texture of the button.
     restartButton.addListener(new ActorGestureListener() {
            public void touchDown(InputEvent event, float x, float y, int pointer, int button) {
                GameInit();}});
}


public void antAct1(){
         for(int i = 1; i < 4; i++){AntAnimate.add(new AntAnimation(app));}

         AntAnimate.get(1).setPosition(x,y);
         stageEnemies.addActor.(AntAnimate.get(1));
         AntAnimate.get(1).addAction(//some actions);

          AntAnimate.get(2).setPosition(x,y);
         stageEnemies.addActor.(AntAnimate.get(2));
         AntAnimate.get(2).addAction(//some actions);

          AntAnimate.get(3).setPosition(x,y);
         stageEnemies.addActor.(AntAnimate.get(3));
         AntAnimate.get(3).addAction(//some actions);

}

public void antAct2(){
     //almost the same with antAct1()
}

public void antAct3(){
     //almost the same with antAct1()
}

public void coinScattered(){
     //almost the same with antAct1()
}

第一次显示屏幕时,会自动调用

推荐答案

show().

init()方法是您自己创建的一种. 您可以随心所欲地调用它. 您不应该仅将所有内容都设置好,得分= 0,player.setPosition(0,0)等都加载到init()中的所有资产.

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.

因此,当您第一次调用init()时,请设置所有变量.再次调用init()时,请再次进行设置. (重新设置设置)

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

对问题的进一步

在从GameInit()调用的方法中,您加载已经加载的文件,并将actor添加到这些actor已经存在并因此重叠的阶段.您还可以添加操作并创建已经创建的对象.

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.

所有GameInit()应该做的就是为那些已经创建的对象设置值. 所以基本上:

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;
}

对象的加载和创建不应重复进行.尝试仅添加我的GameInit()方法,将其命名为GameReset(),然后从您的重置按钮进行调用.

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.

这篇关于如何使用Init()方法重新启动LibGDX中的屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆