Removing then Adding Event Listeners

Hi guys,

Is there any best practice to use when removing an event listener, and then adding a new one?

For example, I have

 local function dragIt(event)  
 event.target.x = event.x  
 event.target.y = event.y  
 end  
  
 local function moveIt(event)  
 if event.phase == "began" then  
  
 event.target:removeEventListener("touch", moveIt)  
 print("Listener Removed")  
  
  
 event.target.x = \_W / 2  
 event.target.y = \_H / 2  
  
  
 event.target:addEventListener("touch", dragIt)  
 print("Listener Added")  
  
 end  
  
 end  
  
object1:addEventListener("touch", moveIt)  
  

The object, when clicked will move to the middle of the screen, and then become draggable, but not movable.
However I can’t seem to get it to work!
Thanks in advance.
[import]uid: 40538 topic_id: 9280 reply_id: 309280[/import]

don’t know what you want exactly probably as per i understand it suppose to like this

module(…, package.seeall)

function new()
local localGroup = display.newGroup()

local rect = display.newRect(0,0,50,50)

local function myff1(e)
if “moved” == e.phase then
e.target.x = e.x
e.target.y = e.y
end
end

local function myff()
rect.x = 240
rect.y = 160
rect:removeEventListener(“tap”,myff)
rect:addEventListener(“touch”,myff1)
end
rect:addEventListener(“tap”,myff)
return localGroup
end

tested with director class [import]uid: 12482 topic_id: 9280 reply_id: 33889[/import]

Thanks for that hgvyas123 - it didn’t even cross my mind to use “tap”.
So I take it it’s not possible to remove a touch event listener, and then add a new touch event listener?
Just out of curiosity :stuck_out_tongue: [import]uid: 40538 topic_id: 9280 reply_id: 33918[/import]

possible dude just replace tap with touch and see it will not create any problem by the way i had not tried that but it will work [import]uid: 12482 topic_id: 9280 reply_id: 33920[/import]

It’s working fine…
Never works when I first try it! [import]uid: 40538 topic_id: 9280 reply_id: 33923[/import]