Adding and Removing listeners with unspecified functions

Say I have a listener I want to to take a parameter in so I do this

b:addEventListener( “collision”, function() foo(b )end )

How do I later on remove said listener? Or is there another way I should be adding in the first place?

Instead of using an anonymous function, you can simply give the function a name, and keep track of it. You can store the function in some local or global variable, or make it a property of the object itself, like in this example:

object.someListener = function(event) foo(event, someotherParameter) end object:addEventListener("collision", object.someListener) object:removeEventListener("collision", object.someListener)

You cannot use anonymous functions with addEventListner if you intend to remvoeEventListner later.  It will always generate a different address for the function and then it can’t find it to remove it.  You have to have a dedicated function and _memo’s solution will work.

Rob

Instead of using an anonymous function, you can simply give the function a name, and keep track of it. You can store the function in some local or global variable, or make it a property of the object itself, like in this example:

object.someListener = function(event) foo(event, someotherParameter) end object:addEventListener("collision", object.someListener) object:removeEventListener("collision", object.someListener)

You cannot use anonymous functions with addEventListner if you intend to remvoeEventListner later.  It will always generate a different address for the function and then it can’t find it to remove it.  You have to have a dedicated function and _memo’s solution will work.

Rob