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!