Add eventlistener to table?

I am currently creating an app where I’m spawning squares. The squares should spin 90 degrees if you tap on it. The problem with my code is that I can’t add an eventlistener to a table. The code looks like this:

local squares = {} local currentLevel = { { 1, 2, 1}, { 3, 2, 3} } local function spinSquare( event) transition.to( event.target, { time=1000, rotation = event.target.rotation + 90}) end for i = 1, #currentLevel do squares[i] = {} for j = 1, #currentLevel[i] do squares[i][j] = display.newRect( 100 \* j, 100 \* i, 50, 50) end end squares:addEventListener( "tap", spinSquare)

I have solved the problem by adding eventlisteners in the loop to every single square using this line:

squares[i][j]:addEventListener( "tap", spinSquare)

I am no expert in corona or programming at all, but this solution feels very ‘cheap’ according to me. It can also be a problem when the table is bigger.

Another solution I tried was by using a display group, and I think it’s the way to go, but then I had another problem. I used this line in the loop:

squaresGroup:insert( squares[i][j])

The problem with this was that when I pressed one of the squares all of them spun around the 0, 0 coordinates. I understand that it was entire squaresGroup that is the so called ‘event.target’ in this case. How do I solve that?

Thanks in advanced!

Hi @niksam98,

It’s actually very reasonable to add a touch/tap listener to each individual object in this case, and use the same event listener for each (and event.target to perform some action on that object). Alternatively, you could add the listener to the entire group and then, based on the touch/tap location, loop through all objects and check for an intersection point, but I don’t think that would be any more efficient (in fact, it could be less efficient).

Take care,

Brent

Thank you for your help! Just to make sure, this won’t be a problem even if it’s 50 or 100 squares?

Thanks again for your help!

Hi @niksam98,

Yes, 50-100 objects should be fine. And if you decide to use a touch listener instead of a tap (for whatever reason), remember to “return true” so that the touch doesn’t propogate to squares which might lie underneath other squares (I’m not sure if that would happen in your app, but just in case, it’s something to be aware of).

Brent

Hi @niksam98,

It’s actually very reasonable to add a touch/tap listener to each individual object in this case, and use the same event listener for each (and event.target to perform some action on that object). Alternatively, you could add the listener to the entire group and then, based on the touch/tap location, loop through all objects and check for an intersection point, but I don’t think that would be any more efficient (in fact, it could be less efficient).

Take care,

Brent

Thank you for your help! Just to make sure, this won’t be a problem even if it’s 50 or 100 squares?

Thanks again for your help!

Hi @niksam98,

Yes, 50-100 objects should be fine. And if you decide to use a touch listener instead of a tap (for whatever reason), remember to “return true” so that the touch doesn’t propogate to squares which might lie underneath other squares (I’m not sure if that would happen in your app, but just in case, it’s something to be aware of).

Brent