How to detect each title individually?

I have this function and it creates a simple board of rectangles.

But i want to be able to detect each of them.

So I can add a function to each of them

    function whichOne(event)         if event.phase == "began" then             if titles.numberID == 10 then                 print("This is box number 10")             end         end     end          local titles = {}     function createBoard()             for i = 1, 10 do                   for j = 1, 6 do             local titles = display.newRect( sceneGroup, 0, 0, 40, 40 )             titles[i] = titles                   titles.x = 60 \* i                         titles.y = 60 \* j             titles.numberID = (i - 1) \* 6 + j             titles:addEventListener( "touch", whichOne)           end            end     end     createBoard()

the createBoard() function is great, works fine

but the whichOne(event) function I have the idea

but that is the one function I don’t know how to make it work.

I tried with event.target but I didn’t get it.

If I have 36 boxes I want to loop to all of them and print

each number on the terminal…

Thanks for your help

Victor

Since you have a nested for loop, you are overwriting the previous [i] entries.  Try this:

function whichOne(event) if event.phase == "began" then print(event.target.numberID) end end local titles = {} local idx = 1 function createBoard() for i = 1, 10 do for j = 1, 6 do titles[idx] = display.newRect( sceneGroup, 0, 0, 40, 40 ) titles[idx].x = 60 \* i titles[idx].y = 60 \* j titles[idx].numberID = idx titles[idx]:addEventListener( "touch", whichOne) idx = idx + 1 end end end createBoard()

Also notice you don’t need another titles object within the for loop.  Just add them directly to the initial table you created.

This one works really nice, Thank you very much!!!

One more thing that I learn.

Happy New Year 2016!!!

Victor

Since you have a nested for loop, you are overwriting the previous [i] entries.  Try this:

function whichOne(event) if event.phase == "began" then print(event.target.numberID) end end local titles = {} local idx = 1 function createBoard() for i = 1, 10 do for j = 1, 6 do titles[idx] = display.newRect( sceneGroup, 0, 0, 40, 40 ) titles[idx].x = 60 \* i titles[idx].y = 60 \* j titles[idx].numberID = idx titles[idx]:addEventListener( "touch", whichOne) idx = idx + 1 end end end createBoard()

Also notice you don’t need another titles object within the for loop.  Just add them directly to the initial table you created.

This one works really nice, Thank you very much!!!

One more thing that I learn.

Happy New Year 2016!!!

Victor