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?
Comments
it should be:
for example:
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:
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
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