Libgdx Scene2d:表上的事件监听器

uda*_*day 3 java event-listener libgdx scene2d

我想知道如何检测桌面上的触摸事件.虽然我尝试过使用inputlistener.但它只让我触及元素.但相反,我希望触摸可以在桌子上的任何地方被检测到.总之,我想把我的桌子变成Button.

Mad*_*nyo 9

您可以向舞台上的任何actor添加侦听器.在你的情况下,我会建议一个ClickListener.您可以将ClickListener添加为"匿名类",就像我在下面所做的那样.clicked用你的逻辑覆盖它的方法你可以摆脱调用,.super因为基本方法什么都不做.您可以访问提供的特定详细信息,InputEvent例如按下哪个按钮.

    Table table = new Table();
    //Might just be this line to have the table interact.
    table.setTouchable(Touchable.enabled); 
    t.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            System.out.println("I got clicked!");
        }
    });
Run Code Online (Sandbox Code Playgroud)

当然,您的表需要扩展才能单击它.如果您无法单击它,则应运行table.debug()并确保在屏幕上看到表格边界并在其中单击.