How to dynamically add a listener?

Hi,

I wrote this test code.

The idea is that the circle flows out of the screen, removes itself and then creates its copy back onto the canvas and adds the tap listener. It throws an error. What am I doing wrong?

-- background local background background = display.newRect(0,0,1000,1200) background:setFillColor( 1 ) -- end background -- Create a circle local kolko kolko = display.newCircle( 200, 200, 60 ) kolko:setFillColor( 0.6,0.6,0 ) -- Destroy and recreate the circle on transition finished local function kolkofinished(obj) kolko:removeSelf( ) kolko = display.newCircle( 200, 200, 60 ) kolko:setFillColor( 0.6,0.6,0 ) kolko:addEventListener( "tap", tapkolko ) end -- Tap listener for the circle: start transition local function tapkolko (event) transition.to(kolko,{time=2000, y=-200, onComplete=kolkofinished}) end kolko:addEventListener( "tap", tapkolko )

Thanks!

Could you post the error you are getting?

If I had to hazard a guess, I’d say you’re running into a problem with having your tapkolko() function scoped after another function that calls it. If you move tapkolko() above kolkofinished() that might work better.

If I move it above kolkofinished() then the transition doesn’t “see” the onComplete listener and nothing happens on transition finished. But it might be the right track.

Here’s the error message:

coronaerror.png

Sorry, I meant scope it above the kolkofinished() function:

-- background local background local tapkolko local kolkofinished background = display.newRect(0,0,1000,1200) background:setFillColor( 1 ) -- end background -- Create a circle local kolko kolko = display.newCircle( 200, 200, 60 ) kolko:setFillColor( 0.6,0.6,0 ) -- Destroy and recreate the circle on transition finished function kolkofinished(obj) kolko:removeSelf( ) kolko = display.newCircle( 200, 200, 60 ) kolko:setFillColor( 0.6,0.6,0 ) kolko:addEventListener( "tap", tapkolko ) end -- Tap listener for the circle: start transition function tapkolko (event) transition.to(kolko,{time=2000, y=-200, onComplete=kolkofinished}) end kolko:addEventListener( "tap", tapkolko )

Aah, now I get it. Genius! I’m always forgetting this with Corona. Trying it out now!

Cool, now it works. Thank you!

Could you post the error you are getting?

If I had to hazard a guess, I’d say you’re running into a problem with having your tapkolko() function scoped after another function that calls it. If you move tapkolko() above kolkofinished() that might work better.

If I move it above kolkofinished() then the transition doesn’t “see” the onComplete listener and nothing happens on transition finished. But it might be the right track.

Here’s the error message:

coronaerror.png

Sorry, I meant scope it above the kolkofinished() function:

-- background local background local tapkolko local kolkofinished background = display.newRect(0,0,1000,1200) background:setFillColor( 1 ) -- end background -- Create a circle local kolko kolko = display.newCircle( 200, 200, 60 ) kolko:setFillColor( 0.6,0.6,0 ) -- Destroy and recreate the circle on transition finished function kolkofinished(obj) kolko:removeSelf( ) kolko = display.newCircle( 200, 200, 60 ) kolko:setFillColor( 0.6,0.6,0 ) kolko:addEventListener( "tap", tapkolko ) end -- Tap listener for the circle: start transition function tapkolko (event) transition.to(kolko,{time=2000, y=-200, onComplete=kolkofinished}) end kolko:addEventListener( "tap", tapkolko )

Aah, now I get it. Genius! I’m always forgetting this with Corona. Trying it out now!

Cool, now it works. Thank you!