Input definition for a group with overlapping transparent images

I use a group to store some images and draw them in SpriteBatch. Now I want to determine which image was clicked. For this reason, I am adding an InputListener to the group to receive an event on touch. The incoming InputEvent received a method (getTarget) that returns a reference to the pressed Actor.

If I click on the transparent area of ​​the actor, I want to ignore the incoming event. And if the actor is behind him, I want to use it instead. I thought of something like this:

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 }; }); 

Is this the right thing to do? I thought that returning false for the touchDown method would continue to propagate for the event, and also let me also get touchDown events for other participants in the same position. But this seems like a misunderstanding ...

UPDATE

PTs answer solves the problem of getting the right event. Now I am having a problem with detecting pixel transparency. It seems to me that to get access I need an image in the form of a Pixmap. But I do not know how to convert the image to Pixmap. I also wonder if this is a good solution in terms of performance and memory usage.

+7
source share
1 answer

I think you want to override the Actor.hit() method. See scene 2d Hit Detection wiki .

To do this, subclass Image and put your specialization hit there. Something like:

 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; } 

I believe that you cannot accomplish this in the touchDown , because Stage will skip Actors "behind" this (only the "parent" participants will receive the touchDown event if you return false here).

+5
source

All Articles