【问题标题】:Check which actor is pressed? (libgdx/androidstudio)检查哪个演员被按下? (libgdx/安卓工作室)
【发布时间】:2015-04-15 21:04:31
【问题描述】:

我对 android 编程和 libgdx/scene2d 以及所有这些好东西还很陌生。我在屏幕上有四个箭头键,当它们被按下时,我正在四处移动一个“花花公子”。我想看看是否有人知道拥有一个 InputListener 的方法,并且在该侦听器内部有一种方法可以检查舞台上按下了哪个演员,并基于此做一些事情,而不是拥有四个不同的 inputlistener 和 touchup/down 方法,每个文本按钮一个,我只想要一个。这是我所拥有的,需要在这个方法中检查哪个演员被按下。感谢阅读和帮助:D

InputListener inlis = new InputListener(){//Creating an InputListener to assign to each button instead of writing the same code four times :D
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("Press");
            return true;
        }

        @Override
        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("Release");
        }
    };
    tbRight.addListener(inlis);
    tbLeft.addListener(inlis);
    tbDown.addListener(inlis);
    tbUp.addListener(inlis);

【问题讨论】:

  • 拥有 4 个不同的侦听器可能更容易。但是你试过event.getRelatedActor()吗?
  • 我环顾四周,没有发现任何东西。我将如何在 if 语句中使用它来检查按下了哪个演员?
  • 最简单的方法是给每个演员一个名字并检查event.getRelatedActor()的名字。但最好为此创建一个新类并对其进行参数化。

标签: android button libgdx actor stage


【解决方案1】:

一次编写每段代码是一个很好的愿望。你可以写一次,然后用四次(甚至更多次)。

1) 使用InputListener 方法创建基类:

public class Button extends Actor {
    int key;

    ... // All other Actor methods goes here

    public void addListeners() {
        mouseListener = new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                doAction();
                return true;
            }
        };
        addListener(mouseListener);

        keyboardListener = new InputListener() {
            @Override
            public boolean keyDown(InputEvent event, int keyCode) {
                if (event.getKeyCode() == key) {
                    doAction();
                    return true;
                } else {
                    return false;
                }
            }
        };
        getStage().addListener(keyboardListener);

    protected void doAction() {
        // This method is empty. We will override it later.
        // You can declare it abstract if you want.
    }
}

2) 然后用每个按钮的所需功能扩展它,如下所示:

public class DownButton extends Button {
    public DownButton(float x, float y, int buttonSize) {
        super(x, y, buttonSize);
        key = Input.Keys.DOWN;
    }

    @Override
    protected void doAction() {
        System.out.println("Go down");
    }
}

3) 最后,您可以将此Actor 添加到您的舞台:

    ...
    DownButton downButton = new DownButton(100, 100, buttonSize);
    stage.addActor(downButton);
    downButton.addListeners();
    ...

在这种情况下,您只需编写 1 行代码即可为每个新按钮添加侦听器。

【讨论】:

  • 这样他仍然需要实现 4 个不同的按钮。只需给Button 一个参数以获取所需的键,并在按下按钮时执行额外的Task
  • 同意。这是一个很好的建议。但在你的情况下,他仍然需要实现 4 个不同的 Task 对象。无论如何,他只会根据需要实现一次侦听器。
猜你喜欢
  • 2015-10-05
  • 1970-01-01
  • 2017-07-11
  • 2018-12-15
  • 2018-01-08
  • 2013-07-08
  • 2017-04-17
  • 2014-04-30
  • 1970-01-01
相关资源
最近更新 更多