object:removeEventListener does not work.

None of my other threads have ever been responded to, but where else am I going to ask for help?

During the buildout phase of my game, when one of my game objects is being created, I add an event listener using (code very much abbreviated):

star[n].collision = stopOverlap  
star:addEventListener( "collision", star[n])  

for now, stopOverlap is something like

print("stopOverlap just fired.")  

a few seconds after my buildout phase, I do something like:

function killStarListeners()  
 local n = 0  
 local i = 1  
 print("killing listeners now")  
 for i = 1, 500 do  
 n = n + 1  
 if star[n] and star[n].removeSelf then  
 star[n]:removeEventListener("collision", stopOverlap)  
 activeListeners = activeListeners - 1  
 print("activeListeners (after live): "..activeListeners)  
 end  
 end  
end  
  
timer.performWithDelay( 10000, killStarListeners, 1)  

When I run this, I get all the expected messages in the console showing that killStarListeners is firing as expected.

Unfortunately, this listeners are really still there, and I still get the “stopOverlap” messages even after the listeners were supposedly removed.

Please advise, as this is a deal-breaker, and one of only a few things standing in my way for releasing my game. [import]uid: 13529 topic_id: 6897 reply_id: 306897[/import]

WTB working edit button.

I know the second line of code should be star[n]:addEventListener. That was a typo, not the source of my bug.
[import]uid: 13529 topic_id: 6897 reply_id: 24104[/import]

Hmm you removed the object before removing the listener:

if star[n] and star[n].removeSelf then

^^ if star[n] isn’t false or nil
AND
star[n].removeSelf() suceeded then

star[n]:removeEventListener(“collision”, stopOverlap)

^^ remove the event listener, but the reference star[n] is now nil.

You should do:

if star[n] then
star[n]:removeEventListener(“collision”, stopOverlap)
star[n].removeSelf()
[import]uid: 8872 topic_id: 6897 reply_id: 24116[/import]

Thanks Kam, but that doesn’t seem to be the issue. Also, I am trying to remove the listeners from the objects that have NOT been removed.
[import]uid: 13529 topic_id: 6897 reply_id: 24124[/import]

I added a .status property to the stars when the are removed, and changed the lister removal code to:

function removeStarListeners()  
 local n = 0  
 local i = 1  
 print("killing listeners now")  
 for i = 1, 500 do  
 n = n + 1  
 if star[n].status == "dead" then  
 print("skipping star"..n)  
 else  
 star[n]:removeEventListener("collision", stopOverlap)  
 star[n].alpha = 0.1  
 activeListeners = activeListeners - 1  
 print("removing listener of: "..star[n].id)  
 print("activeListeners (after live): "..activeListeners)  
 end  
 end  
end  

As expected, after 10 seconds all the stars that have not been removed fade to alpha 0.1. Unfortunately, their listeners are still there. This is driving me crazy! Help me, Carlos! [import]uid: 13529 topic_id: 6897 reply_id: 24125[/import]

Hmm afaik listeners are not garbage collected if you just removeSelf(). So you either way you shouldn’t be calling that until you remove them.

Maybe you need to nil out stars[n].collision first.

I dont use that method of adding/removing listeners in my game… i do it like this:

star[n]:addEventListener( “collision”, stopOverlap)

and

star[n]:removeEventListener( “collision”, stopOverlap)

One is global and one is local, i forget which way round they are. But the above works fine for me. [import]uid: 8872 topic_id: 6897 reply_id: 24126[/import]

Hmm there’s actually nothing wrong with your code. I suspect it may be a bug because you have so many listeners.

Can you try the same with a lot less stars like 10 or 20 just to confirm thats the case? [import]uid: 8872 topic_id: 6897 reply_id: 24127[/import]

PS you could try to skype him:

http://developer.anscamobile.com/forum/2011/02/10/my-skype

He was on irc earlier but he’s gone now. [import]uid: 8872 topic_id: 6897 reply_id: 24128[/import]

Kam, I owe you a cookie!

I was confusing global and local listeners. Apparently, if you want to remove them later from a function, use:

star[n]:addEventListener( "collision", functionName)  

not:

star[n].collision = functionName  
star[n]:addEventListener( "collision", star[n])  

Anyone know where I can read more about why this is? I’m still very new to LUA & Corona.
[import]uid: 13529 topic_id: 6897 reply_id: 24129[/import]

never mind…

seems to be working

c [import]uid: 24 topic_id: 6897 reply_id: 24130[/import]

great, glad u got it working :slight_smile:

Carlos, you can go to sleep now :stuck_out_tongue: [import]uid: 8872 topic_id: 6897 reply_id: 24131[/import]

kam I looked at your code and I was ok that should work… that’s the proper way…

thanks

[import]uid: 24 topic_id: 6897 reply_id: 24132[/import]

One problem I see in

if star[n] and star[n].removeSelf then  

The star[n].removeSelf is not removing the object but returning a property of the object that will be nil if you haven’t set it before. If you want to remove the object, you need to use star[n]:removeSelf(). Since that method doesn’t return any value, you need to test for a property of the display object to see if it has been removed. star[n].x will be nil if the object has been removed. [import]uid: 7559 topic_id: 6897 reply_id: 24504[/import]

Thanks for the reply.

I think I pulled that together from a box2d forum. I was under the impression that star[n] would return true if the object still existed, and star[n].removeSelf would return true if the object was available to be removed.

In hindsight, that doesn’t make much sense.

As I noted above, the fix was based on local vs global listeners, which I’m still fuzzy on, yet managed to make work.
[import]uid: 13529 topic_id: 6897 reply_id: 24506[/import]

If you use a table when adding a listener then use that same table when you remove it. Pretty straightforward really; you were adding a table listener but trying to remove a listener function.

So if you write “star[n]:addEventListener( “collision”, star[n])” then the remove call is “star[n]:removeEventListener( “collision”, star[n])”

Notice that the second line is a copy-paste of the first line with the word “add” changed to “remove.” Like I said, pretty straightforward. [import]uid: 12108 topic_id: 6897 reply_id: 24524[/import]

Thanks Jhocking.

BTW- are you the same Jhocking from the PAX East forums?
[import]uid: 13529 topic_id: 6897 reply_id: 24534[/import]

no

drat somebody else already took my name, now what do I do if I join that forum [import]uid: 12108 topic_id: 6897 reply_id: 24546[/import]