I have objects created from a for loop and a table of values, when each object spawns I would like to be able to click on them and be able to see its object name. So far I have included an event listener within the for loop in the hope that it applies to every object, however what happens is it works for the first object I touch - after that it doesn’t work .
Cheers
The Loop
local colorsData = {} local colorsData = { {name = "blue", img = "images/RectangleBlue@2x.png"}, {name = "red", img = "images/RectangleRed@2x.png"}, {name = "green", img = "images/RectangleGreen@2x.png"} } function levelTapped(self, event) local object = event.target print("Object Name: " .. object.name) end local spaceApart = 110 for idx = 1,9 do local c = idx%3 ~= 0 and idx%3 or 3 local cd = colorsData[c] local p = display.newImageRect(cd.img, 106,60) p.name = cd.name p.x = (idx \* spaceApart) - 365 p.y = display.contentHeight - 30 physics.addBody(p, { isSensor = true }) p.touch = levelTapped p:addEventListener("touch") baseGroup:insert(p) end