Add eventListener to an Image

So i started new project with game based psyhics and everything loaded up as it shoud. I am begginer in the corona language and the best way i can learn is through an examples… I want to make the falling box which is this piece of code

-- make a box(off-screen), position it, and rotate slightly local box = display.newImageRect( "box.png", 40, 40 ) box.x, box.y = 160, -100 box.rotation = 33

to be clickable. Can anyone show me an example where should i add eventListeners or how can i make multiple boxes fall off-screen but without repeating the code… i copyed this code and pasted like 10 times to make 10 boxes fall from the sky but i know thats not correct way to do it.

this is inside level1.lua file as i said i created new project with “Physics-Based Game” options

Hello,

Try this example:

local function doSomething ( event ) --this happens when you tap each box local thisBox = event.target thisBox.x = 160 --return to original x (for example) end for i = 1, 10, 1 do --do 10 times -- make a box(off-screen), position it, and rotate slightly local box = display.newImageRect( "box.png", 40, 40 ) box.x, box.y = 160, -100 box.rotation = 33 box:addEventListener ( "tap", doSomething ) end

Google for Lua (name of language) and “for loops”.

Hello,

Try this example:

local function doSomething ( event ) --this happens when you tap each box local thisBox = event.target thisBox.x = 160 --return to original x (for example) end for i = 1, 10, 1 do --do 10 times -- make a box(off-screen), position it, and rotate slightly local box = display.newImageRect( "box.png", 40, 40 ) box.x, box.y = 160, -100 box.rotation = 33 box:addEventListener ( "tap", doSomething ) end

Google for Lua (name of language) and “for loops”.