【问题标题】:How to restart screen in LibGDX using Init() methods?如何使用 Init() 方法在 LibGDX 中重新启动屏幕?
【发布时间】:2017-08-15 17:29:05
【问题描述】:

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

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 提供的答案的回应,该答案似乎可以解决问题,但需要额外的代码。这里的问题是,无论何时重新启动游戏,重新启动屏幕上的演员都位于前一个屏幕(重新启动前的屏幕)上的演员之上。这是我的代码的新 sn-p,显示了如何使用我的 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()
}

【问题讨论】:

  • 我通常会这样做的方式是创建一个新的 Screen 对象,例如setScreen(new LevelOneScreen());
  • 您也可以只重置位置得分等值
  • 为什么不使用构造函数的内容创建 init 方法,全部来自构造函数和按钮侦听器?

标签: android libgdx screen restart scene2d


【解决方案1】:

show() 在屏幕首次显示时自动调用。

init() 方法是您自己创建的。 你可以随心所欲地称呼它。 您不应该在 init() 中加载所有资产,只需设置所有内容, score = 0, player.setPosition(0,0) 等。

因此,当您第一次调用 init() 时,您设置了所有变量。当您再次调用 init() 时,您再次设置它们。 (重新设置)

问题编辑后:

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

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

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() 并从重置按钮调用它。

【讨论】:

  • 我按照你说的做了,但是一旦我重新启动它只会与屏幕的先前条件重叠,例如硬币显示两次,而另一批仅延迟了几秒钟。跨度>
  • 我必须查看您正在使用的代码才能知道发生了什么。
  • 我的互联网连接中断了几天,但可以。
猜你喜欢
  • 1970-01-01
  • 2021-12-26
  • 2022-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多