Remove event listener while using inline function?

I’m wondering if there is a way to remove an event listener while using an inline function? For example

 

local ball = display.newImage("ball.png") ball:addEventListener("tap", function() return true end)

I want to remove the tap event from the ball but not sure if that’s possible?

You cannot use anonymous functions if you want to remove them. The way Lua processes that is it says “Oh here is this function that doesn’t have an assigned address, let me make up one.” That memory address is passed to addEventListener and stored in a table.

Later when you want to remove it, Lua sees another anonymous function, assigns it a different address. Now when removeEventListener takes that address, it can’t find it in the table of listeners to remove.

Simply use a named function so that you’re passing the same address for the function to both add and removeEventListener.

Rob

You cannot use anonymous functions if you want to remove them. The way Lua processes that is it says “Oh here is this function that doesn’t have an assigned address, let me make up one.” That memory address is passed to addEventListener and stored in a table.

Later when you want to remove it, Lua sees another anonymous function, assigns it a different address. Now when removeEventListener takes that address, it can’t find it in the table of listeners to remove.

Simply use a named function so that you’re passing the same address for the function to both add and removeEventListener.

Rob