2013-05-25 50 views
7

我使用组来存储一些图像并将它们绘制到SpriteBatch。现在我想检测点击了哪个图像。出于这个原因,我将一个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 
     }; 
    }); 

这是正确的方法吗?我认为返回假的方法touchDown将继续传播的事件,并让我也收到touchDown事件的其他角色在同一位置。但是,这似乎是一场误会......

UPDATE

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

+0

你不会从图像到Pixmap。你从Pixmap到纹理到图像可绘制。 – NateS

+0

您是否尝试过我提供的解决方案? – ManishSB

回答

5

我想你想重写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侦听器内做到这一点,因为舞台将会跳过了演员“背后的”这一个(唯一的“家长”的演员,如果你返回false在这里将得到触下事件) 。

相关问题