具有重叠透明图像的组的输入检测

Luc*_*ann 8 libgdx

我使用组来存储一些图像并将它们绘制到SpriteBatch.现在我想检测单击了哪个Image.因此,我向Group添加了一个InputListener,以便在触摸时获取事件.传入的InputEvent有一个方法(getTarget),它返回对被点击的Actor的引用.

如果我点击Actor的透明区域,我想忽略传入的事件.如果背后有一个Actor,我想用它代替.我想过这样的事情:

    myGroup.addListener(new InputListener() {
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            Actor targetActor = event.getTarget();
            // is the touched pixel transparent: return false
            // else: use targetActor to handle the event and return true
        };
    });
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?我认为为方法touchDown会继续传播事件而返回false ,让我也在同一位置接收其他Actors的touchDown事件.但这似乎是一种误解......

UPDATE

PT答案解决了获得正确事件的问题.现在我有问题来检测命中像素是否透明.在我看来,我需要Image作为Pixmap来获取访问权限.但我不知道如何将图像转换为Pixmap.我也想知道这在性能和内存使用方面是否是一个很好的解决方案.

P.T*_*.T. 6

我想你想要覆盖这个Actor.hit()方法.查看scene2d Hit Detection wiki.

为此,子类Image,并将您的hit专业化放在那里.就像是:

public Actor hit(float x, float y, boolean touchable) {
  Actor result = super.hit(x, y, touchable);
  if (result != null) { // x,y is within bounding box of this Actor
     // Test if actor is really hit (do nothing) or it was missed (set result = null)
  }
  return result;
}
Run Code Online (Sandbox Code Playgroud)

我相信你无法在touchDown听众中完成这个,因为舞台将跳过这个"后面"的演员(如果你在这里返回假,只有"父"演员将获得touchDown事件).