removeEventListener

Hi,

I have the following code (thanks @nich sherman) after 3 clicks i need to stop the addEventListener… how and where?

Best regards

Juan

  1. local buttonText = {1,2,3,4,5}
  2. local touchButton = function (event)
  3.       local obj = event.target
  4.       local bid = obj.id
  5.       if event.phase == “ended” then
  6.            
  7.             print (“Button “…bid…” was pressed”)
  8.       end
  9. end
  10. for a = 1, #buttonText, 1 do
  11.       local g = display.newGroup()
  12.       local i = display.newRect(g, 0,0,100, 40)
  13.       local t = display.newText(g, buttonText[a],0, 0, native.systemFont, 10)
  14.       g.x = 160
  15.       g.y = a * 48
  16.       g:addEventListener(“touch”,touchButton)
  17.       g.id = a
  18. end
  19.     

Simply put:

obj:removeEventListener(“touch”,touchButton)

in your event listener when you are ready to remove it.   You could also leave it be and just use a flag to indicate that its disabled and your event listener could check to see if its disabled and simply “return true” if it is.

Rob

local touchButton = function (event)         local obj = event.target       local bid = obj.id         if event.phase == "ended" then                         print ("Button "..bid.." was pressed")             obj:removeEventListener("touch",touchButton)       end   end

or

Thank you Rob

Best regards

Juan

Simply put:

obj:removeEventListener(“touch”,touchButton)

in your event listener when you are ready to remove it.   You could also leave it be and just use a flag to indicate that its disabled and your event listener could check to see if its disabled and simply “return true” if it is.

Rob

local touchButton = function (event)         local obj = event.target       local bid = obj.id         if event.phase == "ended" then                         print ("Button "..bid.." was pressed")             obj:removeEventListener("touch",touchButton)       end   end

or

Thank you Rob

Best regards

Juan