How to remove event listener and add another one on the same object

Hi

What I want to do in my app is on tap of an object transition it to the centre of the screen and then when its tapped again remove it. I have an eventlistener on the object which calls a function to reposition it and then inside that function I have a removeEventListener and another addEventListener to call another function to transition the object back off the screen. I thought this would work but instead on tap of the object is running both functions at once. 

How do I get it to just call one function until I tap on it again. Heres the two functions:

touchPhoto = function ( event ) timer.pause( timer1 ) timer.pause( timer2 ) tapId = event.target.id print(tapId.."one") photosTable[tapId]:removeEventListener( "tap", touchPhoto ) transition.to( photosTable[tapId], {rotation = 360, width =200, height = 200, x= centerX, y = centerY} ) physics.removeBody(photosTable[tapId]) photosTable[tapId]:toFront() photosTable[tapId]:addEventListener( "tap", anotherTouch ) end anotherTouch = function ( event ) timer.resume( timer1 ) timer.resume( timer2 ) tapId = event.target.id print(tapId.."two") transition.to( photosTable[tapId], {rotation = 0, width =200, height = 200, x= centerX+1000, y = centerY+1000} ) end

Hey marshallp24,

added tap and touch listeners are called if they are added inside the listener functions of the same object.

I would suggest one of the following options:

  1. Add the second event listener after the transition has happend. (utilize the onComplete property of transition.to)

  2. If you want the listener to be changed emediatly, use a single listener function for both situations (and skip the removing and reapllying) and add a additional property to the event target to check how often it has been touched.

    touchPhoto = function ( event ) local touchNum = event.target.touchNum or 0 if touchNum == 0 then event.target.touchNum = 1 --resolve the actions of the first touch else event.target.touchNum = 0 --resolve the actions of the second touch end end

https://docs.coronalabs.com/api/type/EventListener/addEventListener.html#gotchas

Perhaps “You cannot” there should read “Problems will arise if you” (because clearly, you can!). Or, even better, the simulator should emit an error message for violating this constraint. Best: remove the constraint. :slight_smile:

Hey marshallp24,

added tap and touch listeners are called if they are added inside the listener functions of the same object.

I would suggest one of the following options:

  1. Add the second event listener after the transition has happend. (utilize the onComplete property of transition.to)

  2. If you want the listener to be changed emediatly, use a single listener function for both situations (and skip the removing and reapllying) and add a additional property to the event target to check how often it has been touched.

    touchPhoto = function ( event ) local touchNum = event.target.touchNum or 0 if touchNum == 0 then event.target.touchNum = 1 --resolve the actions of the first touch else event.target.touchNum = 0 --resolve the actions of the second touch end end

https://docs.coronalabs.com/api/type/EventListener/addEventListener.html#gotchas

Perhaps “You cannot” there should read “Problems will arise if you” (because clearly, you can!). Or, even better, the simulator should emit an error message for violating this constraint. Best: remove the constraint. :slight_smile: