Properly Removing Object On Post Collision

In my game I have collisions in which I need to break objects and make them disappear. I am using the Director Class. Here is a stripped down sample of my code.
[lua]local function onPostCollision( event )
if event.force > 2 then
event.object1:removeSelf()
–I have also tried if event.object1 then event.object1:removeSelf end
end
end
Runtime:addEventListener(“postCollision”, onPostCollision)
–later on when changing scene
Runtime:removeEventListener(“postCollision”, onPostCollision)[/lua]

It works the first time, but when the stage is reloaded or revisited via Director, I get this error:

ERROR: Attempt to remove an object that has already been removed from the stage or whose parent/ancestor group has already been removed.

How do I fix this? Thanks [import]uid: 54716 topic_id: 15159 reply_id: 315159[/import]

Furthermore, I noticed that the Runtime function does not turn off when I leave the scene. I have added a delay with the timer in my code and after the first time, I noticed that the pieces start being removed and the delay is nonexistent. [import]uid: 54716 topic_id: 15159 reply_id: 56044[/import]

I am just a beginner, but wouldn’t it be better to have the listener attached to the object instead?

When you destroy objects, you have to destroy the listener as well!

Joakim [import]uid: 81188 topic_id: 15159 reply_id: 56270[/import]

I thought I tried that and it is extremely slow. I am certain that global listener is the better choice. I can’t argue with the performance boost.

I still can’t figure out how remove and add the event listener properly. Help please. [import]uid: 54716 topic_id: 15159 reply_id: 56347[/import]

Just do

  
Runtime:removeEventListener ( "Postcollision", onPostCollision )  
  

before you remove your object, and not after :slight_smile:

[import]uid: 81188 topic_id: 15159 reply_id: 56354[/import]

Thanks for the replies info642 but you logic is incorrect and only reinforces the problem I was having. When I use your solution, the objects should stop breaking after the first piece breaks, but it didn’t, the pieces continued to break and the collisions were still being calculated.

I fixed the problem by directly attaching the collision function to the Runtime table.
[lua]function Runtime:postCollison( event )
[…]
end
Runtime:addEventListener(“postCollision”, Runtime)
[…]
Runtime:removeEventListener(“postCollision”, Runtime)[/lua]

Can anyone explain why I couldn’t remove the event with a ‘normal’ variable. [import]uid: 54716 topic_id: 15159 reply_id: 56397[/import]

The more objects you have that you’d like to listen for postCollisions, the better it is to use a global runtime listener. For a few objects, it’s better to do individual listeners for the objects and (if necessary) stop propagation. [import]uid: 36054 topic_id: 15159 reply_id: 56416[/import]

I am having the same issue, have you resolved it?

Thanks,
Andrea. [import]uid: 70929 topic_id: 15159 reply_id: 95721[/import]

I solved it.

Using Director Class, there are some problem removin Event Listeners with Runtime:removeEventListener(“blabla”,blabla).
I found this and it worsk:

function removeAllListeners(obj)  
 obj.\_functionListeners = nil  
end  
  
removeAllListeners(Runtime) -- or object if is an object listener  

Hope this may help someone.
Andrea [import]uid: 70929 topic_id: 15159 reply_id: 95742[/import]