simple question about eventListener syntax

Hi, what would the code look like if I wanted to check if a listener was ‘true’, ie; ‘hadn’t been removed’?

I have a function that removes listeners after a collision, but the function isn’t always called… So when I clean up my scene (for StoryBoard) I get an error, because in some circumstances I’m trying to remove an event listener that I’ve already removed with a function.

Thanks

That is very strange. I often have exitScene code that removes listeners that I know for a fact have already been removed, and I don’t get any errors thrown. I’m assuming these are Runtime listeners you’re removing? Can you post the removal code you’re using?

Hi Alex, actually no, they’re not runtime listeners, they are attached to objects, and additionally those objects are in a table (workaround for 60 upvalues issue)…

Cleanup code is going in storyboard’s scene:exitScene section and looks like this:

function scene:exitScene( event ) local group = self.view vars[1]:removeEventListener( "collision", vars[1] ) end

As part of the cleanup, I’m having to remove the objects too (as there are bodies and joints attached to them)… So in this instance ‘vars[1]’ is being removed as well as it’s associated listener.

If you’re keeping a reference ANYWHERE to an object, it won’t be fully removed till ALL references are set to nil.

Note: Not yelling, just making sure you see the important words.

In your post, you said you are keeping a reference to these objects in a table.  Make sure to clear that table when you exit or you’ll keep a bunch of references active.

Note: I find the best way to handle this is as follows:

local myObjs = {} function scene:exitScene( event ) ... myObjs = {} ... end

When creating objects I do this:

local anObj = \<object builder here\> anObj.collision = function( self, event ) if( event.phase = "ended" ) then self:removeEventListener( "collision" ) myObjs[self] = nil end return true end anObj:addEventListner( "collision" ) myObjs[anObj] = anObj

That is very strange. I often have exitScene code that removes listeners that I know for a fact have already been removed, and I don’t get any errors thrown. I’m assuming these are Runtime listeners you’re removing? Can you post the removal code you’re using?

Hi Alex, actually no, they’re not runtime listeners, they are attached to objects, and additionally those objects are in a table (workaround for 60 upvalues issue)…

Cleanup code is going in storyboard’s scene:exitScene section and looks like this:

function scene:exitScene( event ) local group = self.view vars[1]:removeEventListener( "collision", vars[1] ) end

As part of the cleanup, I’m having to remove the objects too (as there are bodies and joints attached to them)… So in this instance ‘vars[1]’ is being removed as well as it’s associated listener.

If you’re keeping a reference ANYWHERE to an object, it won’t be fully removed till ALL references are set to nil.

Note: Not yelling, just making sure you see the important words.

In your post, you said you are keeping a reference to these objects in a table.  Make sure to clear that table when you exit or you’ll keep a bunch of references active.

Note: I find the best way to handle this is as follows:

local myObjs = {} function scene:exitScene( event ) ... myObjs = {} ... end

When creating objects I do this:

local anObj = \<object builder here\> anObj.collision = function( self, event ) if( event.phase = "ended" ) then self:removeEventListener( "collision" ) myObjs[self] = nil end return true end anObj:addEventListner( "collision" ) myObjs[anObj] = anObj