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]
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]
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]
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]
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]
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]