How to Pick one sprite out of two on same location with higher zorder

Hi all,

As the title suggest i want to select one sprite for drag out of 2 or more sprites at the same location. but what happens is touch detects collision only with bigger rect which is always back of smaller sprite, which is on front. What i want is i want to select the sprite which is higher on zorder or visually front.

I’m not sure exactly how to go about this, but you might look into propagation and swallowing touches. If you can not swallow touches and have each touch propagate through all sprites under the touch location. Then on touch began you could have the touch handlers just flag sprites that were under the touch or add them to a list. On update check for all sprites that were touched and find the one with the largest zorder and set it as “selected”. On touch move you can just move the selected sprite. On touch end you can reset selected sprite to null.

You could also handle touches in a more global manner by using a root Node that covers all sprites that can be touched and implementing a touch event listener for that Node instead of touch handlers on each sprite. Add each draggable sprite as children of this root node.

I would probably go with the root node global touch handler as it feels like the solution with less code and likely more easy to understand the state at every step.

There’s likely other drag & drop like discussion on this forum as well.