EventListener for each instance of an object

Hi, I’ve created a function to create an overlay circle and spawn the circle 6 times.

These overlays are then repositioned on the screen.

My question is, how do I add EventListeners to each of these 6 instances of the circle object.

Imagine that they are all 6 different buttons and I want to make it so that when a user touched an instance of a circle, it pops up different messages.

Thank you.

overlayTable = {} --// FUNCTION: Create overlay local function createOverlay()     circleOverlay = display.newCircle(\_W, \_H, 45);     return circleOverlay end     -- Create 6 overlays:     for i = 1, 6 do     overlayTable[i] =  createOverlay() end      -- Reposition overlays: overlayTable[1].x = (\_W \* 0.5) - 150;   overlayTable[1].y = (\_H \* 0.5) - 50; overlayTable[2].x = (\_W \* 0.5) - 155;   overlayTable[2].y = (\_H \* 0.5) + 65; overlayTable[3].x = (\_W \* 0.5) + 3;     overlayTable[3].y = (\_H \* 0.5) - 80; overlayTable[4].x = (\_W \* 0.5) - 3;     overlayTable[4].y = (\_H \* 0.5) + 60; overlayTable[5].x = (\_W \* 0.5) + 160;   overlayTable[5].y = (\_H \* 0.5) - 60; overlayTable[6].x = (\_W \* 0.5) + 155;   overlayTable[6].y = (\_H \* 0.5) + 60;  
overlayTable = {} --// FUNCTION: Create overlay local function createOverlay() circleOverlay = display.newCircle(\_W, \_H, 45); circleOverlay:addEventListener("tap",circleOverlayTap) return circleOverlay end

jonPM’s code will add the same listener to all the circles. In the shared circle listener, to find out which one was hit (so you can give different messages), you could do something like:

[lua]

function circleOverlayTap(event)

local circleHit = nil

for i = 1, 6 do
    if( overlayTable[i] ==  event.target ) then

        circleHit = i

        break

    end

    if( circleHit ~= nil ) then

        – Give a unique message for this circle

    end

end

[/lua]

Alternately, when creating the circle, you can just hang special data (messages) right off the structure (just be sure you don’t overwrite an existing table member), as in modifying your creation loop thus::

[lua]

– Create 6 overlays:    
for i = 1, 6 do
    overlayTable[i] =  createOverlay()

    overlayTable[i].specialMessage = "This is circle # " … i
end

[/lua]

Thank you. Both answers work great!

overlayTable = {} --// FUNCTION: Create overlay local function createOverlay() circleOverlay = display.newCircle(\_W, \_H, 45); circleOverlay:addEventListener("tap",circleOverlayTap) return circleOverlay end

jonPM’s code will add the same listener to all the circles. In the shared circle listener, to find out which one was hit (so you can give different messages), you could do something like:

[lua]

function circleOverlayTap(event)

local circleHit = nil

for i = 1, 6 do
    if( overlayTable[i] ==  event.target ) then

        circleHit = i

        break

    end

    if( circleHit ~= nil ) then

        – Give a unique message for this circle

    end

end

[/lua]

Alternately, when creating the circle, you can just hang special data (messages) right off the structure (just be sure you don’t overwrite an existing table member), as in modifying your creation loop thus::

[lua]

– Create 6 overlays:    
for i = 1, 6 do
    overlayTable[i] =  createOverlay()

    overlayTable[i].specialMessage = "This is circle # " … i
end

[/lua]

Thank you. Both answers work great!