I’m curious as to how many people can answer this without running the code. What will the following print when the user taps the rect?
local button = display.newRect( display.contentCenterX, display.contentCenterY, 200, 200) for i = 1, 100 do local closure\_i = i button:addEventListener("tap", function() print ("I am tap listener " .. closure\_i) end) end
I’m also curious as to what people think should actually happen. I am torn between liking and being frustrated with what actually happens. Given the documentation does not go into detail, is this intended behaviour?
For bonus points will the following behave differently?
local button = display.newRect( display.contentCenterX, display.contentCenterY, 200, 200) local count = 0 local tapListener = function() count = count + 1 print ("I am static tap listener and have been called " .. count .. " times") end for i = 1, 100 do button:addEventListener("tap", tapListener) end