Accessing Display Objects

Hi people, first time poster on (any) coding forum. Usually like to go and hunt the problem down myself but as I’m fairly new to Corona/Lua and time is of the essence on this project thought I’d post and see if anyone can point me in the right direction.

I have a for loop that creates display objects based on what part of the game the user is on: 

for j=1, numAnswers do

                local answerBtnGroup = “quest”…i…“Ans”…j…“Gp”

                answerBtnGroup = display.newGroup()

                answerName:insert(answerBtnGroup)

                

                – Add background image

                local answerBtnName = “quest”…i…“Ans”…j

                answerBtnName = display.newSprite(answerBtnGroup, numberBtnSheet, numberBtnSqData)

                answerBtnName:setSequence(“normal”)

                answerBtnName:setReferencePoint(display.TopLeftReferencePoint)

                answerBtnName.y = 0

                answerBtnName:setFillColor(236,229,122)

                

                local answerTextName = “quest”…i…“Ans”…j…“Text”

                answerTextName = display.newText(answerBtnGroup, “24”, 0, 0, textFont, numberAnswerFontSize)

                answerTextName:setReferencePoint(display.TopCenterReferencePoint)

                answerTextName.x, answerTextName.y = 0,35

                answerTextName:setTextColor(0)

                

                if j == 1 then

                    answerBtnGroup.x = 0

                else

                    answerBtnGroup.x = (answerBtnName.width + (spacing/4)) * (j-1)

                end

– These are supposed to be drag and drop type objects, (boxes with numbers on them), my problem is if they are created inside the for loop how/where do I implement touch events on them with…

function answerBtnGroup:touch( event )    

                – set up local variables to handle focus on object        

                local answerBtnGroup = event.target  

                local phase = event.phase 

        if event.phase == “began” then

… rest of code 

answerBtnGroup:addEventListener(“touch”, answerBtnGroup)

– end 

Any help would be greatly appreciated. As a side note, if create the display objects individually the touch event function works…

Thanks

Think I might have it; must have had a brain freeze, declare: local answerBtnGroup before the for loop and 

answerBtnGroup:addEventListener(“touch”, answerBtnGroup)

                function answerBtnGroup:touch( event )    

after it?

Is that correct practice?

Is this what you’re looking for?

local stage = display.getCurrentStage() local function touch(e)     local target, phase, hasFocus, offset = e.target, e.phase, e.target.hasFocus, e.target.offset          if (phase == "began") then         stage:setFocus(target)         target.hasFocus = true         target.offset = {x=e.x-target.x, y=e.y-target.y}         return true     elseif (target.hasFocus) then         if (phase == "moved") then             target.x, target.y = e.x-offset.x, e.y-offset.y         else             stage:setFocus(nil)             target.hasFocus = false         end         return true     end     return false end for i=1, 10 do     local item = display.newGroup()     item.index = i     item.x, item.y = 100, i\*30     item.rect = display.newRect( item, 0, 0, 100, 20 )     item.rect:setFillColor(100+i\*10,100+i\*10,100+i\*10)     item.text = display.newText( item, "Item: "..i, 2, 2, native.systemFont, 12 )     item.text:setTextColor(0,0,0)     item:addEventListener("touch",touch) end  

Think I might have it; must have had a brain freeze, declare: local answerBtnGroup before the for loop and 

answerBtnGroup:addEventListener(“touch”, answerBtnGroup)

                function answerBtnGroup:touch( event )    

after it?

Is that correct practice?

Is this what you’re looking for?

local stage = display.getCurrentStage() local function touch(e)     local target, phase, hasFocus, offset = e.target, e.phase, e.target.hasFocus, e.target.offset          if (phase == "began") then         stage:setFocus(target)         target.hasFocus = true         target.offset = {x=e.x-target.x, y=e.y-target.y}         return true     elseif (target.hasFocus) then         if (phase == "moved") then             target.x, target.y = e.x-offset.x, e.y-offset.y         else             stage:setFocus(nil)             target.hasFocus = false         end         return true     end     return false end for i=1, 10 do     local item = display.newGroup()     item.index = i     item.x, item.y = 100, i\*30     item.rect = display.newRect( item, 0, 0, 100, 20 )     item.rect:setFillColor(100+i\*10,100+i\*10,100+i\*10)     item.text = display.newText( item, "Item: "..i, 2, 2, native.systemFont, 12 )     item.text:setTextColor(0,0,0)     item:addEventListener("touch",touch) end