How do you callback the name of a display object inserted in a display group?

Generated using a loop, there are tile display objects laid next to each other. Each tile is assigned a name using tile.name = i, the i starting with 1 and ending with tile number. The tiles are stored in a display group, and a touch handler is added to display group.

When I touch the tile group, I’d like to get a return on the name value of the individual tile touched. But, print(event.target.name) doesn’t work since event.target refers to the display group.

What can I do to refer to the individual tile when touched?

Thanks in advance.

You’ll have to parse all the objects and see which one the <x,y> position is ‘within’ or  add touch listeners to the tiles instead.

Well, if you know the size of the tiles and all tiles are layed out next to each other, you can estimate the tiles index as well.

local tileSize = 50 --width and height of each tile local gridWidth = 10 --number of tiles in one row local function estimateTileIndex(x, y) return math.floor(x/tileSize) + math.floor(y/tileSize)\*gridWidth + 1 end --assume the most upper left tiles upper left corner is at position 0,0 print(estimateTileIndex(130, 270) --returns 53

The above example works, if you lay out your tiles from left to right and from top to bottom.

Try this:

local tileGroup = display.newGroup( ) local tileLis = function ( e ) local currentTile = e.target if (e.phase == "began") then print( currentTile.name ) end end for i = 1, 5 do local currentTile = display.newRect( tileGroup,25, 25+(50\*(i-1)), 50, 50 ) currentTile.name = i currentTile:addEventListener( "touch", tileLis ) end

Thank you all for your responses. They were really helpful. I managed to get it to work.

Below is a demonstration of what I was really trying to do. I am creating a level selection scene that would allow me to swipe from one theme to the next. Please let me know if there’s anything I could do to improve the code given that my purpose is to create a level selection scene.

local count = 0 local pageTotal = 3 local tileTouchHandler local scroller local w, h = display.contentWidth, display.contentHeight local cX, cY = display.contentCenterX, display.contentCenterY local abs = math.abs local pageScroll = display.newGroup() pageScroll.anchorX = 0; pageScroll.anchorY = 0 local scrollMinX, scrollMaxX = 0, -(pageTotal - 1) \* w local startX = 0 local slideX = 0 local function scroller(event) local pageNum = event.target.name if event.phase == 'began' then slideX = pageScroll.x - event.x elseif event.phase == 'moved' then if scrollMinX \>= pageScroll.x and pageScroll.x \> scrollMaxX then pageScroll.x = event.x + slideX end elseif event.phase == 'ended' then -- Scroller touch released if abs(event.x - event.xStart) \>= 150 then if (event.x - event.xStart) \< 0 then -- Move Right if pageNum ~= pageTotal then transition.to( pageScroll, {time = 300, x = startX - 640}) startX = startX - 640 end else -- Move Left if pageNum ~= 1 then transition.to( pageScroll, {time = 300, x = startX + 640}) startX = startX + 640 end end else transition.to( pageScroll, {time = 300, x = startX}) end end end local pageNum = 0 for i = 1, pageTotal do local page = display.newRect(pageScroll, pageNum \* w, 0, w,h ) page.anchorX = 0; page.anchorY = 0 page.name = i page:addEventListener('touch', scroller) if i == 2 then page:setFillColor(1,0,0,0.5) elseif i == 3 then page:setFillColor(0,0,1,0.5) end pageNum = pageNum + 1 end

You’ll have to parse all the objects and see which one the <x,y> position is ‘within’ or  add touch listeners to the tiles instead.

Well, if you know the size of the tiles and all tiles are layed out next to each other, you can estimate the tiles index as well.

local tileSize = 50 --width and height of each tile local gridWidth = 10 --number of tiles in one row local function estimateTileIndex(x, y) return math.floor(x/tileSize) + math.floor(y/tileSize)\*gridWidth + 1 end --assume the most upper left tiles upper left corner is at position 0,0 print(estimateTileIndex(130, 270) --returns 53

The above example works, if you lay out your tiles from left to right and from top to bottom.

Try this:

local tileGroup = display.newGroup( ) local tileLis = function ( e ) local currentTile = e.target if (e.phase == "began") then print( currentTile.name ) end end for i = 1, 5 do local currentTile = display.newRect( tileGroup,25, 25+(50\*(i-1)), 50, 50 ) currentTile.name = i currentTile:addEventListener( "touch", tileLis ) end

Thank you all for your responses. They were really helpful. I managed to get it to work.

Below is a demonstration of what I was really trying to do. I am creating a level selection scene that would allow me to swipe from one theme to the next. Please let me know if there’s anything I could do to improve the code given that my purpose is to create a level selection scene.

local count = 0 local pageTotal = 3 local tileTouchHandler local scroller local w, h = display.contentWidth, display.contentHeight local cX, cY = display.contentCenterX, display.contentCenterY local abs = math.abs local pageScroll = display.newGroup() pageScroll.anchorX = 0; pageScroll.anchorY = 0 local scrollMinX, scrollMaxX = 0, -(pageTotal - 1) \* w local startX = 0 local slideX = 0 local function scroller(event) local pageNum = event.target.name if event.phase == 'began' then slideX = pageScroll.x - event.x elseif event.phase == 'moved' then if scrollMinX \>= pageScroll.x and pageScroll.x \> scrollMaxX then pageScroll.x = event.x + slideX end elseif event.phase == 'ended' then -- Scroller touch released if abs(event.x - event.xStart) \>= 150 then if (event.x - event.xStart) \< 0 then -- Move Right if pageNum ~= pageTotal then transition.to( pageScroll, {time = 300, x = startX - 640}) startX = startX - 640 end else -- Move Left if pageNum ~= 1 then transition.to( pageScroll, {time = 300, x = startX + 640}) startX = startX + 640 end end else transition.to( pageScroll, {time = 300, x = startX}) end end end local pageNum = 0 for i = 1, pageTotal do local page = display.newRect(pageScroll, pageNum \* w, 0, w,h ) page.anchorX = 0; page.anchorY = 0 page.name = i page:addEventListener('touch', scroller) if i == 2 then page:setFillColor(1,0,0,0.5) elseif i == 3 then page:setFillColor(0,0,1,0.5) end pageNum = pageNum + 1 end