【问题标题】:Input detection for a Group with overlapping transparent Images具有重叠透明图像的组的输入检测
【发布时间】:2013-05-20 21:39:36
【问题描述】:

我使用组来存储一些图像并将它们绘制到 SpriteBatch。现在我想检测点击了哪个图片。出于这个原因,我向 Group 添加了一个 InputListener 以获取触地事件。传入的 InputEvent 有一个方法 (getTarget),该方法返回对单击的 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
        };
    });

这是正确的做法吗?我认为为 touchDown 方法返回 false 将继续传播该事件,并让我也可以在同一位置接收其他 Actors 的 touchDown 事件。但这似乎是个误会……

更新

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

【问题讨论】:

  • 您不会从图像转到像素图。你从 Pixmap 到 Texture 到 Drawable 到 Image。
  • 你试过我提供的解决方案了吗?

标签: libgdx


【解决方案1】:

我认为您想覆盖 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;
}

我相信您无法在 touchDown 侦听器中完成此操作,因为舞台将跳过“后面”的 Actor(如果您在此处返回 false,则只有“父”actor 会收到 touchDown 事件)。

【讨论】:

    猜你喜欢
    • 2015-03-12
    • 2020-10-17
    • 1970-01-01
    • 2020-09-03
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 2013-07-17
    相关资源
    最近更新 更多