首页 > 解决方案 > LibGDX中的屏幕图像重叠对话框

问题描述

我正在尝试在 LibGDX 中创建一个游戏,在游戏中我创建了对话框,当我们按下 Back Pressed 键时会出现该对话框,但是当对话框出现时,来自显示屏的图像与对话框重叠,因此我的内容被隐藏了。 在此处输入图像描述

以下是我的代码

public class GamePlayScreen extends AbstractScreen {
private String TAG = "Game Play Screen";
private SpriteBatch batch;
private OrthographicCamera camera;
private Stage stage;
private int width = 0;
private int height = 0;
private GameDrawer gameDrawer;
private AbstractGameController gameController;


public GamePlayScreen(Assests assests) {
    this.assests = assests;
    camera = new OrthographicCamera();
    width = Gdx.graphics.getWidth();
    height = Gdx.graphics.getHeight();
    camera.setToOrtho(false, width, height);
    camera.update();
    batch = new SpriteBatch();
    FillViewport viewport = new FillViewport(width, height, camera);
    viewport.update(width, height, true);
    stage = new Stage(viewport);
    gameDrawer = new GameDrawer(batch, assests);
    gameController = new GameController(camera, assests);
    Gdx.input.setCatchKey(Input.Keys.BACK, true);

}

public void show() {
    //    Gdx.app.log(TAG, "Enters show method");
    Image backgroundImage = new Image(assests.manager.get(Assests.backgroundImageTexture));
    backgroundImage.setSize(width, height);
    stage.addActor(backgroundImage);
    InputMultiplexer multiplexer = new InputMultiplexer();
    multiplexer.addProcessor(stage);
    multiplexer.addProcessor(new GestureDetector(new TouchController(gameController)));
    Gdx.input.setInputProcessor(multiplexer);
    batch.setProjectionMatrix(camera.combined);


    //   Gdx.app.log(TAG, "Executed show method succussfully");
}

public void render(float delta) {
    //Gdx.app.log(TAG, "Enters render method");
    Gdx.gl.glClearColor(0.187f, 0.246f, 0.621f, 1.0f);
    Gdx.gl.glClear(16384);
    if (Gdx.input.isKeyPressed(Input.Keys.BACK))
        onBackPressed();
    stage.act();
    stage.draw();

    batch.begin();
    gameController.processGameRender();

    gameDrawer.drawDealtDeck(gameController.getDealtDeck());
    gameDrawer.drawDiscardedDeck(gameController.getDiscardedDeck());

    gameDrawer.drawPlayerDeck(gameController);

    batch.end();

    //   Gdx.app.log(TAG, "render metod executed succussfully");
}

@Override
public void onBackPressed() {
    Gdx.app.log(TAG, "Back Key Pressed");
    Skin skin = assests.manager.get(Assests.glassySkin);
    Button btnQuit = new TextButton("Quit", skin);
    Button btnRestart = new TextButton("Restart", skin);
    Button btnCancel = new TextButton("Cancel", skin);
    Dialog dialog = new Dialog("Are you sure you want to exit", skin) {
        @Override
        protected void result(Object object) {
            super.result(object);
        }
    };
    dialog.button(btnQuit);
    dialog.button(btnCancel);
    dialog.button(btnRestart);
    dialog.show(stage);
    dialog.padLeft(50.0f);
    dialog.padRight(50.0f);
    dialog.padBottom(10.0f);
    dialog.show(this.stage);

     /*  Dialog dialog= new Dialog("Are you sure want to Exit",skin){
        @Override
        public float getPrefWidth() {
            return width/2;
        }
        @Override
        public float getPrefHeight() {
           return height/2;
        }
    };
    dialog.setModal(false);
    dialog.setMovable(false);
    dialog.setResizable(false);
    dialog.setFillParent(false);
    btnQuit.addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            Gdx.app.log(TAG,"Quit Button Pressed");
        return true;
        }
    });
    float btnsize=40.0f;
    Table table=new Table();
    table.add(btnQuit).width(btnsize).height(btnsize);
    table.add(btnRestart).width(btnsize).height(btnsize);
    table.add(btnCancel).width(btnsize).height(btnsize);
    dialog.getButtonTable().add(table).center();
    dialog.show(stage).setPosition(width/4,height/4);
    stage.addActor(dialog);
    */


}

}

任何关于如何停止图像重叠的建议都会非常有帮助。

标签: androidlibgdxdialog

解决方案


如果您希望在您的游戏上绘制对话框,那么您必须在方法中绘制您的舞台之前绘制您的游戏render

如果您有必须在某些游戏对象后面绘制的其他阶段对象,您将需要两个阶段。


推荐阅读