Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
how to remove a not fully know event listener ? — Gideros Forum

how to remove a not fully know event listener ?

nat12nat12 Member
edited August 2012 in General questions
Hello,

i have a Button class, it register all listeners for touches evens, and also users of the class add some listeners in this way

but = Button.new(main_play_bitmap, 250, 36)
but:addEventListener("clickUp", self.clickPlay, self)

later i want to use a but:destroy function to free all listeners of the button, this is easy for touches event, that were registered by button so it know all parameters, but i am unable to remove the user added listener...
i tried something similar to

if self.hasEventListener("clickUp") then
self:removeEventListener("clickUp", ?, ?)
end

but the button do not know the 2nd and 3th parameter to pass to the removeEventListener function, any suggestion?

Dislikes: gokhansahin477

+1 -1 (+0 / -1 )Share on Facebook

Comments

  • hello @nat12
    it should be:
    self:hasEventListener("clickUp")
    and not
    self.hasEventListener("clickUp")
    use colon insetad of dot ;)
  • I was curious about this before as well. Is there any way to fetch all event listeners of a certain type on an object?
  • @ar2rsawseen I think @nat12 is referring to that you need to have the function and data passed to an added event listener to remove it, but what if at the time I want to remove an event listener, I don't have a direct reference to them?

    for example:
    btn:addEventListener("clickUp", function() doSomething end, {foo=1,bar=2})
  • ar2rsawseenar2rsawseen Maintainer
    edited August 2012 Accepted Answer
    haha, silly me.
    Yep got it now. But unfortunately there is no way, at least I don't know one.
    You need to pass exactly the same parameters to remove event listener. Same thing goes in javascript. But unless you pass the correct function, it does not know which function to remove (if for example you have multiple of them).

    @nat12 how could user actually add event?

    well usually you save the event handler (and all needed parameters) as property of class, like:
    btn.clickUp = function() 
        doSomething 
    end
    btn.clickUpParams = {foo=1,bar=2}
    btn:addEventListener("clickUp", btn.clickUp, btn.clickUpParams)
     
    -- and then to remove
    btn:removeEventListener("clickUp", btn.clickUp, btn.clickUpParams)
  • techdojotechdojo Guru
    Accepted Answer
    My solution is like @ar2sawseen's - in my custom button class I have a reference to a callback function that is passed when the button is created. Then when the button is pressed the custom callback function is called so that the function that called the button know's when it's been pressed - this neatly hides all the messy lower level event "twiddling" code and leaves a nice clean high level interface, literately
     
    local thisButtonsID = 1
     
    local function wasPressed(button)
       print("Button "..button.id.." was pressed")
    end
     
    myButton = CustomButton( { ... id = thisButtonsID, callback = wasPressed ... } )
    Note : That's just a bit of psuedo code, when creating a custom ui control I usually pass my data in as a table - but you should get the idea.
    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
  • I was hoping there was some way to read back the datas from the EventDispatcher instead to duplicate them in the button class.

    I tried to code a generalization of what ar2rsawseen and techdojo suggested

    function Button:addListener(type, listener, data)
    table.insert(self.listenersTable, { t = type, l = listener, d = data } )
    self:addEventListener(type, listener, data)
    end

    function Button:removeListeners()
    for i = 1, #self.listenersTable do
    local data = self.listenersTable[i]
    self:removeEventListener(data.t, data.l, data.d)
    end
    end

    (Atilim, if you are reading, any chance to have a removeAllListeners() function added to EventDispather in near future?)

    Likes: techdojo

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.