[ Resolved ] Multiple EventListeners

I have 4 PNG files displayed on top of each other:

Big ring
Medium ring
Small Ring
Smallest ring

All 4 have an eventListener to the same function:

Rings[x]:addEventListener(“touch”, RingTouch )

Of course no matter where i tap, the RingTouch function is executed 4 times.

How do I detect wich ring i touched?

Any tips?
[import]uid: 50459 topic_id: 26297 reply_id: 326297[/import]

Give them identifiers:

[code]
ring = display.new…
ring.identifier = “ring1”

–And so on

–in your touch event

if event.target.identifier == “ring1” then – access the id you created

return true --at the bottom of the function (very important) [import]uid: 84637 topic_id: 26297 reply_id: 106576[/import]

The 4 PNG files have the same height/width, at the exact same location on the screen, so the touch event always fires for all 4 of them.

I can identify them, but i need to detect if i touched the outer ring, medium ring, or smallest ring etc.

[import]uid: 50459 topic_id: 26297 reply_id: 106582[/import]

Also consider applying masks to your images to reduce the touch surface to just the non-transparent part. I assume from your description that you’re displaying nested rings with probably the smallest one “on top” and the largest one “on bottom”.

To extend Danny’s suggestion though:

[lua]-- in your touch event

if event.target.identifier == “ring1” then
– the stuff you want to do
elseif event.target.identifier == “ring2” then
– etc
else
return false
end

return true[/lua]

If you [lua]return false[/lua] in the unmatched case the event will trickle down to the next lower object, etc. If you mask your rings you don’t need to worry about it, since if if the unmasked portions don’t overlap the ring you touched is the ring you touched.

Hope that makes sense. I worked out a lot of these same methods in my game “Concentric”: http://developer.anscamobile.com/showcase/concentric [import]uid: 44647 topic_id: 26297 reply_id: 106584[/import]

Thanks, i will try the mask thing, sounds easier :slight_smile:
[import]uid: 50459 topic_id: 26297 reply_id: 106598[/import]