Removing event listeners

Hi all,

I am using an anonymous function to wrap my event listener so that I can pass whatever I would like as a parameter.  It works great, except that I can’t figure out how to remove the stupid thing when I’m done with it. Here is an example of what I am doing:

local table = {} function table.eventlistener (event, p1, p2, p3)      -- do stuff with event, p1, p2, and p3      -- At some point down here, I want to remove the Runtime listener. end local var1, var2, var3 = 1, 2, 3 function table:addeventlistener () Runtime:addEventListener ("enterFrame", function (event) self.eventlistener (event, var1, var2, var3) end) end table:addeventlistener ()

Right now I am using the following to attempt to remove the listener:

Runtime:removeEventListener ("enterFrame", function (event) table.eventlistener (event, var1, var2, var3)

Unfortunately, this does not work at all.  Anyone have thoughts on how to solve this quandary?

EDIT:  I have also tried substituting p1, p2, and p3 for var1, var2, and var3 in table.eventlistener just in case I needed to reference the parameters by their name inside of the function called by the listener.  This didn’t work either.

You cannot use anonymous functions for event listeners if you want to remove them.  The reason for this is that addEventListener stores two values in a table:

  1. The event type (“touch”, “tap”, etc.)

  2. The address to the function to call.

So if you do:  obj:addEventListener(“touch”, function( event ) doSomething(); end), what gets inserted is “touch” and the anonymous address of that function, for instance:

“touch”, 0x92834ab84

When you try and remove it: obj:removeEventListener(“touch”, function( event ) doSomething(); end), the address of the anonymous function is going to be different as it’s different anonymous function. So lets say, the address is 0x8384bbdf.  removeEventListner loops over the table of active listeners and hunts for “touch”, 0x8384bbdf, but it can’t find it since that was never inserted, “touch”, 0x92834ab84 was.

Therefore you cannot use anonymous functions, you have to do fixed functions if you plan to remove them.

Rob

Thanks Rob, I appreciate the quick and concise answer.

You cannot use anonymous functions for event listeners if you want to remove them.  The reason for this is that addEventListener stores two values in a table:

  1. The event type (“touch”, “tap”, etc.)

  2. The address to the function to call.

So if you do:  obj:addEventListener(“touch”, function( event ) doSomething(); end), what gets inserted is “touch” and the anonymous address of that function, for instance:

“touch”, 0x92834ab84

When you try and remove it: obj:removeEventListener(“touch”, function( event ) doSomething(); end), the address of the anonymous function is going to be different as it’s different anonymous function. So lets say, the address is 0x8384bbdf.  removeEventListner loops over the table of active listeners and hunts for “touch”, 0x8384bbdf, but it can’t find it since that was never inserted, “touch”, 0x92834ab84 was.

Therefore you cannot use anonymous functions, you have to do fixed functions if you plan to remove them.

Rob

Thanks Rob, I appreciate the quick and concise answer.