Libgdx:Dialog没有收听输入

时间:2015-10-22 07:59:49

标签: input dialog libgdx

我的目标是向用户显示一个Dialog,以便他能够选择要添加到舞台的Actor。我在实现Screen和GestureListener的应用程序的主屏幕中执行此操作。 (我需要GestureListener来监听双击)。我在覆盖的touchDown()方法中创建了Dialog。 出现对话框时,它不起作用。如果我点击屏幕上的任意一点,屏幕会一直在 touchDown 中收听输入。

这是代码:

public class SquareDefense implements Screen, GestureListener {
    ....
    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {

        System.out.println("touchDown");

        if(squareDefenseTable.getActor(x, y) != null) {
            // rotate the clicked actor!
            squareDefenseTable.rotateActor(x, y);
        }
        else {
            showDialog();
        }

        return true;
    }

    private void showDialog() {
        Dialog dialog = new Dialog("Choose an action", skin) {

            @Override
            protected void result(Object object) {
                boolean exit = (Boolean) object;
                if (exit) {
                    Gdx.app.exit();
                } else {
                    remove();
                }
            }

            @Override
            public Dialog show(Stage stage) {
                return super.show(stage);
            }

            @Override
            public void cancel() {
                super.cancel();
            }

            @Override
            public float getPrefHeight() {
                return 50f;
            }
        };
        dialog.button("Yes", true);
        dialog.button("No", false);
        dialog.key(Input.Keys.ENTER, true);
        dialog.key(Input.Keys.ESCAPE, false);
        dialog.show(stage);
    }
}

1 个答案:

答案 0 :(得分:0)

我想你错过了添加这行代码:

Gdx.input.setInputProcessor(stage);

告诉我这是否有效