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()