remove ALL eventLIsteners from array

Having established how to remove an event listener for one object passed into onTap, we are trying to remove all event listeners passed in from the image table with removeTap()

The code all runs fine, there are no errors. But the eventListeners are not being removed…

removeTap = function( event )     local i     for i = 1, #imageList do         if imageList[i].tap then             print(image[imageList[i].name])             --image.btn\_back:removeEventListener( "tap", onTap )             image[imageList[i].name]:removeEventListener( "tap", onTap )         end     end end

And here is removeTap within the rest of the code:

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 = true, w=114, h=312, x=1944, y=193, }, { name="bm\_clr3", tap = true, 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 local removeTap -- forward declare local function onTap (event) local ot = event.target local phase = event.phase print("tapped") removeTap() -- ot:removeEventListener( "tap", onTap ) -- this remove one tap 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 -- REMOVE TAP removeTap = function( event ) local i for i = 1, #imageList do if imageList[i].tap then print(image[imageList[i].name]) image[imageList[i].name]:removeEventListener( "tap", onTap ) end end end end -- end for i end -- end drawScreen drawScreen()

I modified a few things but it works just fine, you just gotta call removeTap

local imageList = {} local image = {} local drawScreen drawScreen = function() imageList = { { name="bm\_clr1", tap = true, w=114, h=200, x=320, y=209, }, { name="bm\_clr2", tap = true, w=114, h=200, x=320, y=193, }, { name="bm\_clr3", tap = true, w=114, h=200, x=320, y=228, }, { name="bm\_clr4", tap = false, w=114, h=200, x=320, y=262, }, { name="bm\_crossbnd", tap = false, w=118, h=200, x=320, y=210 }, { name="bm\_cross", tap = false, w=68, h=70, x=200, y=300 }, { name="bmclear", tap = false, w=150, h=448, x=200, y=209}, { name="icon\_credits", tap = false, w=68, h=200, x=320, y=280} } 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 local removeTap -- forward declare local function onTap (event) local ot = event.target local phase = event.phase print("tapped") 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 -- REMOVE TAP removeTap = function( event ) local i for i = 1, #imageList do if imageList[i].tap then print(image[imageList[i].name]) image[imageList[i].name]:removeEventListener( "tap", onTap ) end end end timer.performWithDelay(2000, function() removeTap() end) end -- end for i end -- end drawScreen drawScreen()

Thanks for the reply Eja. Unfortunately adding the timer there isn’t working. My fault, I hadn’t put removeTap() inside onTap as it needs to remove tap once a tap is made… not each time the loop occurs.  I tried putting your timer in onTap so it calls when the button is tapped but still no success. No errors but the eventListeners aren’t removed.

I noticed you removed the the imageList from bmType.fc = function() by deleting bmType.fc = function(). This is only the bare bones of the code but there are 6 bmType functions containing imageList tables. Depending on how a variable is defined, one of these functions is called thereby deciding which imageList to use. I didn’t include all of them because they aren’t relevant to the problem here. But imageList should still be contained within bmType.fc to give an idea of scope of the imageList table (which is defined outside drawscreen btw so it can be called outside this function).

I modified a few things but it works just fine, you just gotta call removeTap

local imageList = {} local image = {} local drawScreen drawScreen = function() imageList = { { name="bm\_clr1", tap = true, w=114, h=200, x=320, y=209, }, { name="bm\_clr2", tap = true, w=114, h=200, x=320, y=193, }, { name="bm\_clr3", tap = true, w=114, h=200, x=320, y=228, }, { name="bm\_clr4", tap = false, w=114, h=200, x=320, y=262, }, { name="bm\_crossbnd", tap = false, w=118, h=200, x=320, y=210 }, { name="bm\_cross", tap = false, w=68, h=70, x=200, y=300 }, { name="bmclear", tap = false, w=150, h=448, x=200, y=209}, { name="icon\_credits", tap = false, w=68, h=200, x=320, y=280} } 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 local removeTap -- forward declare local function onTap (event) local ot = event.target local phase = event.phase print("tapped") 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 -- REMOVE TAP removeTap = function( event ) local i for i = 1, #imageList do if imageList[i].tap then print(image[imageList[i].name]) image[imageList[i].name]:removeEventListener( "tap", onTap ) end end end timer.performWithDelay(2000, function() removeTap() end) end -- end for i end -- end drawScreen drawScreen()

Thanks for the reply Eja. Unfortunately adding the timer there isn’t working. My fault, I hadn’t put removeTap() inside onTap as it needs to remove tap once a tap is made… not each time the loop occurs.  I tried putting your timer in onTap so it calls when the button is tapped but still no success. No errors but the eventListeners aren’t removed.

I noticed you removed the the imageList from bmType.fc = function() by deleting bmType.fc = function(). This is only the bare bones of the code but there are 6 bmType functions containing imageList tables. Depending on how a variable is defined, one of these functions is called thereby deciding which imageList to use. I didn’t include all of them because they aren’t relevant to the problem here. But imageList should still be contained within bmType.fc to give an idea of scope of the imageList table (which is defined outside drawscreen btw so it can be called outside this function).