Why does this EventListener stop listening/repeating?

Hello everyone, 

Long-time CoronaSDK daydreamer, first time commenter here. I’m just starting to get into it, but there are quite a few fundamental concepts I’m still struggling with (like cross-device display issues in config.lua, but that’s a topic for a different day). 

Right now I’m trying to figure out why the rectangle created by the following snippet only fades to alpha=.5 once. Why doesn’t the myObject.alpha = 1 statement following the transition.to within the function subsequently restore the alpha to 1 after the transition? 

I’m missing why the EventListener appears to stop working after one tap. It only works on the first tap, not on subsequent taps. 

I know this is a profoundly basic question, but I’m still profoundly ignorant of Corona and Lua at this point. 

Thank you for any help, and it’s very nice to be here. 

--create object local myObject = display.newRect( 0, 0, 400, 400 ) myObject:setFillColor( .3, .3, .7 ) myObject.x = display.contentCenterX myObject.y = display.contentCenterY -- touch listener function local function myObjectlistener( event ) transition.to(myObject, { time=1000, alpha=.5 }) myObject.alpha = 1 return true end -- listener myObject:addEventListener ( "tap", myObjectlistener )

Hi @kermit4karate,

Do you want the alpha to be restored after the transition completes, or before it begins fading to 0.5? If you want it to snap back to alpha 1 after the transition, you’ll need to use an “onComplete” event listener on the transition. What your current code is doing is simply setting the alpha to 1 in the exact same time step (frame) as the transition’s beginning point. That setting will not “wait” until after the transition merely because it’s on the next line.

Hope this helps,

Brent

Hi Brent, 

Thank you. I’m still really struggling with the concepts of scope with regard to how and when chunks execute, and also local and global functions and variables, but I think I understand this one now. I tweaked the above snippet, and now it appears to be working. Thank you again. 

Also, thank you for approving my account. :) 

--create object local myObject = display.newRect( 0, 0, 400, 400 ) myObject:setFillColor( .3, .3, .7 ) myObject.x = display.contentCenterX myObject.y = display.contentCenterY -- touch listener function local function boxFullAlpha( event ) transition.to(myObject, { time=100, alpha=1 }) myObject.x = display.contentCenterX myObject.y = display.contentCenterY end local function myObjectlistener( event ) transition.to(myObject, { time=1000, alpha=.3, onComplete = boxFullAlpha}) end -- listener myObject:addEventListener ( "tap", myObjectlistener )

Hi @kermit4karate,

Yes, that code is correct, and scope is perfect too. :slight_smile:

Brent

Hi @kermit4karate,

Do you want the alpha to be restored after the transition completes, or before it begins fading to 0.5? If you want it to snap back to alpha 1 after the transition, you’ll need to use an “onComplete” event listener on the transition. What your current code is doing is simply setting the alpha to 1 in the exact same time step (frame) as the transition’s beginning point. That setting will not “wait” until after the transition merely because it’s on the next line.

Hope this helps,

Brent

Hi Brent, 

Thank you. I’m still really struggling with the concepts of scope with regard to how and when chunks execute, and also local and global functions and variables, but I think I understand this one now. I tweaked the above snippet, and now it appears to be working. Thank you again. 

Also, thank you for approving my account. :) 

--create object local myObject = display.newRect( 0, 0, 400, 400 ) myObject:setFillColor( .3, .3, .7 ) myObject.x = display.contentCenterX myObject.y = display.contentCenterY -- touch listener function local function boxFullAlpha( event ) transition.to(myObject, { time=100, alpha=1 }) myObject.x = display.contentCenterX myObject.y = display.contentCenterY end local function myObjectlistener( event ) transition.to(myObject, { time=1000, alpha=.3, onComplete = boxFullAlpha}) end -- listener myObject:addEventListener ( "tap", myObjectlistener )

Hi @kermit4karate,

Yes, that code is correct, and scope is perfect too. :slight_smile:

Brent