adding event listeners for objects from a table

A Newbie question from a Newb for you all!

I’m making some progress in my first corona app experience, yay.

I have created a table that stores my image file names.
I have randomly sorted the table, and
I have displayed 3 images from said table onto the screen!
I even have a home screen with a start button that takes you to a new scene before the previously mentioned stuff happens.

Question!

I want to track touches on the three images that are displayed.

I have seen many examples where an eventlistener is added to an on screen item like this…

-- displays the start button  
local startBtn = display.newImage("start.png")  
startBtn.x, startBtn.y = 160, 270  
localGroup:insert( startBtn )  
  
local function start ()  
-- change the scene by going to game.lua  
director:changeScene("game")  
end  
  
-- Start button listen for a tap event, and calls the 'start' function  
startBtn:addEventListener("tap", start)  

But how would I use the same principle when my images all have the same variable name because I have displayed them inside of a loop by picking them from the table?

for i=1, 3 do  
 local imgThing = display.newImage( imageList[i] )  
 imgThing.x = 70  
 imgThing.y = i\*60+200  
 localGroup:insert( imgThing )  
end  

Perhaps I should just assign them all eventlisteners based on their place in the table?

like
imgThing[1]:addEventListener(“tap”, start)
imgThing[2]:addEventListener(“tap”, start)
imgThing[3]:addEventListener(“tap”, start)

?

Or could I do that WITHIN the for loop like

imgThing[i]:addEventListener(“tap”, start)

?

Anyway, I better stop now before I get way off course.

Thanks for any suggestions! [import]uid: 191855 topic_id: 33643 reply_id: 333643[/import]

Looking at more examples it seems that even though my imgThings are coming from a table that I should be able to set things up with one line like this somewhere… but I have tried it in and outside of the loop and it crashes my app every time.

imgThing:addEventListener( “tap”, start )

Still fiddling here. [import]uid: 191855 topic_id: 33643 reply_id: 133794[/import]

Hi @jnewbie,
This line should work, *within* the loop, say, after line 5 in your loop code example above. Just make sure that your “start” function resides above the loop, because Lua needs to up-reference it when it creates those listeners within the loop.

imgThing:addEventListener("tap", start)

During the loop, you’ll also need to add the 3 items (or whatever amount) to a separate holding table. Why? Because when you exit the scene, you must loop through those items (now in the holding table) and REMOVE the tap event listeners. Failure to do so will leave the listeners in memory and cause a gradual memory leak, the bane of all programmers.

Best regards,
Brent
[import]uid: 200026 topic_id: 33643 reply_id: 133807[/import]

THANK YOU

The up referencing bit is what was destroying my attempt - I didn’t know about that! I had my function at the bottom of my file.

Thanks I never would have caught that.

The memory leak bit is very important too – I never would have thought of that one. Thanks a ton. Will keep that in mind and be sure to address it before I’m done with this project (if ever) [import]uid: 191855 topic_id: 33643 reply_id: 133816[/import]

Looking at more examples it seems that even though my imgThings are coming from a table that I should be able to set things up with one line like this somewhere… but I have tried it in and outside of the loop and it crashes my app every time.

imgThing:addEventListener( “tap”, start )

Still fiddling here. [import]uid: 191855 topic_id: 33643 reply_id: 133794[/import]

Hi @jnewbie,
This line should work, *within* the loop, say, after line 5 in your loop code example above. Just make sure that your “start” function resides above the loop, because Lua needs to up-reference it when it creates those listeners within the loop.

imgThing:addEventListener("tap", start)

During the loop, you’ll also need to add the 3 items (or whatever amount) to a separate holding table. Why? Because when you exit the scene, you must loop through those items (now in the holding table) and REMOVE the tap event listeners. Failure to do so will leave the listeners in memory and cause a gradual memory leak, the bane of all programmers.

Best regards,
Brent
[import]uid: 200026 topic_id: 33643 reply_id: 133807[/import]

THANK YOU

The up referencing bit is what was destroying my attempt - I didn’t know about that! I had my function at the bottom of my file.

Thanks I never would have caught that.

The memory leak bit is very important too – I never would have thought of that one. Thanks a ton. Will keep that in mind and be sure to address it before I’m done with this project (if ever) [import]uid: 191855 topic_id: 33643 reply_id: 133816[/import]