removeEventListener NOT removing event listener.

Not sure why this simple function doesn’t work. If the button in question (image.bm2) is tapped, it continues to print(“tapped”) from onTap even though there is a removeEventListener in there…

 onTap = function(event) local ot = event.target local phase = event.phase print("tapped") ot:removeEventListener( "tap", onTap ) return true end -- ADD TAP if imageList[i].tap then image.bm2:addEventListener( "tap", onTap ) end

I just ran this code without any problems:

local function onTap (event) local ot = event.target local phase = event.phase print("tapped") ot:removeEventListener( "tap", onTap ) return true end -- ADD TAP local rect = display.newRect(100, 100, 100, 100) rect:addEventListener( "tap", onTap ) 

What is image.bm2? Specifically bm2? Can you post some more code? It seems that you want to loop over a table but then add the event listener to a sub value fo the image or something. Some more would code would definitely help.

Best regards,

Tomas

Thanks for taking a look. Here is the code boiled down to its structure.

local bmType = {} local imageList = {} local image = {} local drawScreen drawScreen = function() bmType.fc = function() imageList = { { name="bm\_clr1", tap = true, w=114, h=418, x=1944, y=209, }, { name="bm\_clr2", tap = false, w=114, h=312, x=1944, y=193, }, { name="bm\_clr3", tap = false, w=114, h=314, x=1944, y=228, }, { name="bm\_clr4", tap = false, w=114, h=312, x=1944, y=262, }, { name="bm\_crossbnd", tap = false, w=118, h=420, x=1944, y=210 }, { name="bm\_cross", tap = false, w=68, h=70, x=1944, y=300 }, { name="bmclear", tap = false, w=150, h=448, x=1944, y=209}, { name="icon\_credits", tap = false, w=68, h=106, x=1944, y=280} } end bmType[fc]() -- call bmType for i = 1, #imageList do image[imageList[i].name] = display.newRect ( imageList[i].x, imageList[i].y, imageList[i].w, imageList[i].h ) -- onTap function onTap = function(event) local ot = event.target local phase = event.phase print("tapped") ot:removeEventListener( "tap", onTap ) return true end -- ADD TAP if imageList[i].tap then image[imageList[i].name]:addEventListener( "tap", onTap ) -- Make the button instance respond to touch events end end end -- end drawScreen drawScreen()

First, is this even called:

bmType[fc]() -- call bmType

I think it should be:

bmType["fc"]() -- call bmType

Regarding the actual problem, try changing it to a local function instead:

local function onTap (event)

Best regards,

Tomas

What happens is in your code the onTap is a global variable and you keep on over writing it. On all your boxes there is one that get tapped once and then deleted and that is the last box created. For the remaining images “onTap” is now referencing to another onTap event, which is removed correctly, but it does not reference to the onTap for that particular image.

I hope that make sense, I’m a little bit to tired to write a better explanation.

Best regards,

Tomas

Yes, that’s it thanks Tomas :slight_smile:

I had forward declared onTap but it was outside drawScreen.

Also did try localising earlier with:

local onTap = function(event)

But that didn’t work. local function onTap (event) does though. Not sure of the difference between those 2…

Thanks again.

I just ran this code without any problems:

local function onTap (event) local ot = event.target local phase = event.phase print("tapped") ot:removeEventListener( "tap", onTap ) return true end -- ADD TAP local rect = display.newRect(100, 100, 100, 100) rect:addEventListener( "tap", onTap ) 

What is image.bm2? Specifically bm2? Can you post some more code? It seems that you want to loop over a table but then add the event listener to a sub value fo the image or something. Some more would code would definitely help.

Best regards,

Tomas

Thanks for taking a look. Here is the code boiled down to its structure.

local bmType = {} local imageList = {} local image = {} local drawScreen drawScreen = function() bmType.fc = function() imageList = { { name="bm\_clr1", tap = true, w=114, h=418, x=1944, y=209, }, { name="bm\_clr2", tap = false, w=114, h=312, x=1944, y=193, }, { name="bm\_clr3", tap = false, w=114, h=314, x=1944, y=228, }, { name="bm\_clr4", tap = false, w=114, h=312, x=1944, y=262, }, { name="bm\_crossbnd", tap = false, w=118, h=420, x=1944, y=210 }, { name="bm\_cross", tap = false, w=68, h=70, x=1944, y=300 }, { name="bmclear", tap = false, w=150, h=448, x=1944, y=209}, { name="icon\_credits", tap = false, w=68, h=106, x=1944, y=280} } end bmType[fc]() -- call bmType for i = 1, #imageList do image[imageList[i].name] = display.newRect ( imageList[i].x, imageList[i].y, imageList[i].w, imageList[i].h ) -- onTap function onTap = function(event) local ot = event.target local phase = event.phase print("tapped") ot:removeEventListener( "tap", onTap ) return true end -- ADD TAP if imageList[i].tap then image[imageList[i].name]:addEventListener( "tap", onTap ) -- Make the button instance respond to touch events end end end -- end drawScreen drawScreen()

First, is this even called:

bmType[fc]() -- call bmType

I think it should be:

bmType["fc"]() -- call bmType

Regarding the actual problem, try changing it to a local function instead:

local function onTap (event)

Best regards,

Tomas

What happens is in your code the onTap is a global variable and you keep on over writing it. On all your boxes there is one that get tapped once and then deleted and that is the last box created. For the remaining images “onTap” is now referencing to another onTap event, which is removed correctly, but it does not reference to the onTap for that particular image.

I hope that make sense, I’m a little bit to tired to write a better explanation.

Best regards,

Tomas

Yes, that’s it thanks Tomas :slight_smile:

I had forward declared onTap but it was outside drawScreen.

Also did try localising earlier with:

local onTap = function(event)

But that didn’t work. local function onTap (event) does though. Not sure of the difference between those 2…

Thanks again.