如何正确切换屏幕?

时间:2019-09-28 15:57:43

标签: java libgdx screen render

我是libgdx的新手,我正在尝试切换屏幕,但是我无法正确切换屏幕,我看到了类似的问题,但都无济于事(

我希望屏幕可以正常显示,但是菜单屏幕上仍然保留着另一个屏幕上的飞船

this is how it should be

this is how it turned out

public class MenuScreen extends Game implements Screen {

...

@Override
public void show() {

...

 play_disabled = new Image(new Texture(Gdx.files.internal("menuButtons/play_disabled.png")));
Menu = new Stage(viewport, batch);

...

Menu.addActor(play_disabled);

...

play_disabled.addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int 
pointer, int button) {
            play_enabled.setVisible(true);
            setScreen(new GameScreen());
            return true;
        }
});

...

 }
}



public class GameScreen extends Game implements Screen{
...
private SpaceShip ship;
...
@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0,0,0,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        batch.draw(bg, 0, 0, movement / 4, -240, 800, 720);
        batch.draw(bg2, 0, 0, movement / 2, -240, 800, 720);
        batch.draw(bg3, 0, 0, movement, -240, 800, 720);

        if(is_paused){
            ship.draw(batch);
            ship.drawUI(is_paused);
        }
        else{
            movement += (200 * delta);
            ship.drawUI(is_paused);
            ship.draw(batch);
            ship.doLogic();
        }
        batch.end();
}

...

}

public class SpaceShip extends GameObject{

...

private GameUi controller;

...

public void drawUI(boolean is_paused){
    controller.draw(is_paused);
}

...

}




public class GameUi extends Game{

public GameUi(Texture background, Texture knob, float deadZoneRadius, float x, float y, float width, float height, Polygon shipBounds, Texture bulletTexture){

...

exit_button = new Image(new Texture(Gdx.files.internal("exit.png")));

...

exit_button.addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            setScreen(new MenuScreen());
            GameScreen.is_paused = false;
            return true;
        }
    });

...

}

public void draw(boolean is_paused) {
    batch.end();
    stage.draw();
    stage.act(Gdx.graphics.getDeltaTime());
    batch.begin();
    font_numbers.getData().setScale(0.3f);
    font_numbers.setColor(Color.CYAN);
    font_numbers.draw(batch, "148932", 537, 467, 100,1, false);
    if(is_paused) {
        batch.draw(PauseBg, 0 ,0 , 800, 480);
    }
    batch.end();
    if (is_paused){
        PauseStage.draw();
        PauseStage.act(Gdx.graphics.getDeltaTime());
    }
    super.render();
    batch.begin();
   }
}

0 个答案:

没有答案