removeEventListener not working

 Hello. n[count5]:removeEventListener(“touch”, sceneStart) works only for touched object (n[count5]) but not for the other. Any ideas?

function scene:show(event) if "did" == event.phase then for i = 1, #myData.type do if myData.category[i] == "category1" then count = count + 1 if myData.type[i] == "video" then video = video + 1 local function sceneStart(event) if "began" == event.phase then newVideo = native.newVideo(display.contentCenterX, display.contentCenterY, 1280, 720) newVideo:load("url"..event.target.name, media.RemoteSource) newVideo:addEventListener("video", videoListener) newVideo.id = i newVideo.count2 = count for q = 1, #myData.type do if myData.category[q] == "category1" then count5 = count5 + 1 n[count5]:removeEventListener("touch", sceneStart) end end return true end end n[count]:addEventListener("touch", sceneStart) n[count].name = "video"..video..".mp4" n[count].id = count elseif myData.type[i] == "image" then image = image + 1 local function sceneStart(event) if "began" == event.phase then display.loadRemoteImage("url"..event.target.name, "GET", networkListener, event.target.name, system.TemporaryDirectory, display.contentCenterX, display.contentCenterY) id = event.target.id for q = 1, #myData.type do if myData.category[q] == "category1" then count5 = count5 + 1 n[count5]:removeEventListener("touch", sceneStart) end end count5 = 0 return true end end n[count]:addEventListener("touch", sceneStart) n[count].name = "image"..image..".png" n[count].id = count end end end end end

When you do a call like:

object:addEventListener( “touch”, myTouchFunction )

Which internally turns into:

object:addEventListener( “touch”, 0x939ab48c )

Where 0x939ab48c is the address to the function. The memory address  will vary with each function. What’s happening behind the scene is that there is a table that holds the object, the type of event and the address to the function. When you go to remove an event listener, it takes the object,  the event type and the address to the function. If it can’t find something that matches all three of those values, it returns and doesn’t do any thing.

In your code above, when you wrote:

local function sceneStart(event)

It’s nested in side 3 “if” statements, a “for” statement and inside the scene:show() function. Each if statement, for statement and the function represents a block. When you declare a variable “local”, it’s only visible to the block it’s declared in. So only that inner “if” knows about a function named sceneStart(). The net effect when you go to remove the event, the value of sceneStart is nil  because that remove isn’t in the same  block of code.

This is called “scope” and we have a tutorial on it:  https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

The fix is to move that function outside of all of those if’s and for’s and outside of scene:show() into the main chunk of module. Then you can declare it local but it’s visible to the entire scene.

Rob

Thanks Rob :slight_smile:

When you do a call like:

object:addEventListener( “touch”, myTouchFunction )

Which internally turns into:

object:addEventListener( “touch”, 0x939ab48c )

Where 0x939ab48c is the address to the function. The memory address  will vary with each function. What’s happening behind the scene is that there is a table that holds the object, the type of event and the address to the function. When you go to remove an event listener, it takes the object,  the event type and the address to the function. If it can’t find something that matches all three of those values, it returns and doesn’t do any thing.

In your code above, when you wrote:

local function sceneStart(event)

It’s nested in side 3 “if” statements, a “for” statement and inside the scene:show() function. Each if statement, for statement and the function represents a block. When you declare a variable “local”, it’s only visible to the block it’s declared in. So only that inner “if” knows about a function named sceneStart(). The net effect when you go to remove the event, the value of sceneStart is nil  because that remove isn’t in the same  block of code.

This is called “scope” and we have a tutorial on it:  https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

The fix is to move that function outside of all of those if’s and for’s and outside of scene:show() into the main chunk of module. Then you can declare it local but it’s visible to the entire scene.

Rob

Thanks Rob :slight_smile: