Event Listener problem

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
local cd = { {name = "blue", img = "images/RectangleBlue.png"}, {name = "red", img = "images/RectangleRed.png"}, {name = "green", img = "images/RectangleGreen.png"} } function levelTapped(self, event) if( event.phase ~= "ended" ) then return false print("Object Name: " .. self.cd.name) return true end local spaceApart = 110 for i = 1, 9 do local c = 1 + i % 3 local p = display.newImageRect( cd[c].img, 106,60) p.cd = cd[c] p.x = ( i \* spaceApart ) - 365 p.y = display.contentHeight - 30 physics.addBody(p, { isSensor = true }) p.touch = levelTapped p:addEventListener("touch") baseGroup:insert(p) end

@Roaminggamer: Thanks for that code, it works great. Is it possible to drag the entire display.group has a whole and still have each object collide individually? 

You can drag a display group, but that will goof up the physics.

i.e. If you have object A in a group and object B in another group, then you drag object A’s group, the offset will cause physics collision issues.

In short, physics collisions only work properly if all objects in all groups have the same group position and rotation context.

So, you need to come up with another way to move multiple objects via a single drag.

Sorry

I see, thanks for the advice. Is it possible to transition them to the left or right and maintain collision that way? Im guessing they would need to be moved manually by name and not by the ‘P’ value ?

local cd = { {name = "blue", img = "images/RectangleBlue.png"}, {name = "red", img = "images/RectangleRed.png"}, {name = "green", img = "images/RectangleGreen.png"} } function levelTapped(self, event) if( event.phase ~= "ended" ) then return false print("Object Name: " .. self.cd.name) return true end local spaceApart = 110 for i = 1, 9 do local c = 1 + i % 3 local p = display.newImageRect( cd[c].img, 106,60) p.cd = cd[c] p.x = ( i \* spaceApart ) - 365 p.y = display.contentHeight - 30 physics.addBody(p, { isSensor = true }) p.touch = levelTapped p:addEventListener("touch") baseGroup:insert(p) end

@Roaminggamer: Thanks for that code, it works great. Is it possible to drag the entire display.group has a whole and still have each object collide individually? 

You can drag a display group, but that will goof up the physics.

i.e. If you have object A in a group and object B in another group, then you drag object A’s group, the offset will cause physics collision issues.

In short, physics collisions only work properly if all objects in all groups have the same group position and rotation context.

So, you need to come up with another way to move multiple objects via a single drag.

Sorry

I see, thanks for the advice. Is it possible to transition them to the left or right and maintain collision that way? Im guessing they would need to be moved manually by name and not by the ‘P’ value ?