Removing Enter Frame Listeners

Hi Everyone,

I’m trying to find out if there is a better way to solve this problem.

I’ve got an object module (class) that returns an instance of a display object. Within that module, an enterFrame event listener is added to Runtime with that new object as it’s reference.

So back in our game.lua, where our new objects are created, I want to move on to a new scene (when the game is over for example). Do I have to cycle through each reference to remove the Runtime event listener?

The reason I ask is because I’ve actually got a number of modules, and there could potentially be hundreds of objects, so therefore I would have to create a cycle for each object type.

Perhaps there’s a way to cycle through all the Runtime object’s event Listeners? and cancel the enterFrame ones?

Thanks in advance for your help guys.

Michael

Removing a display object should remove its listener. If you really do need to cycle through hundreds of objects every frame I would suggest managing the list yourself and having only one listener.

Thanks @horacebury, But this object that contains the event listeners is Runtime, since the event listeners are “enterFrame” listeners.

Is there any way to access Runtime’s Listeners?

Those of you looking for an answer here may find this post interesting.

http://www.ludicroussoftware.com/blog/2011/08/24/remove-all-listeners/

However, it’s advised to heed his warnings that might have an affect on how the code is read in future iterations of Corona.

I wonder could any Corona staff clarify on the safety of removing listeners in this way?
 

Your object should have its own destroy function and that function should deal with the runtime removal. That way your caller doesn’t have to worry about it. In fact if you read this past Tuesdays blog post, it talks about overriding built in functions. You could make your own removeSelf that removes the runtime listener then it would call the factory removeSelf.

Ah! I had actually written my own custom removeMe() function in the object’s module and removed the runtime listener within that, but didn’t know how to override corona’s default call to my new custom function.

Thanks Rob, I’ll take another look at the blog post and activate my removeMe() function.

Right, I’m sorry; When I replied earlier I was extremely tired. I should make more sense now.

That said, I still think I was correct in both statements - they just weren’t as linked as I thought because you’re talking about Runtime listeners which would not be removed when a display object is removed from the system.

Moving on, Rob is exactly right in that you can easily override the removeSelf function to remove your objects’ own Runtime listeners.

It’s just IMHO though that you would be better served to add all similar objects to the same display group and have a single Runtime enterFrame listener loop over them. This has the benefit of more easily managed objects and not needing to remove the listeners for each display object; You simply remove the listener at the end of the scene.

@horacebury I do understand your point and I actually have neatly contained similar objects into display groups, but there is still a need for me to create individual modules of objects as there are different types within the over all game.

Having created my own removeSelf() function within the object, when called directly this function works fine, but when removing objects by removing their parent display group, the individual objects’ removeSelf() function is not actually called it seems.

Are there any other suggestions on this method? I am trying to keep the code as clean an easily understood for beginners as possible and to have the object be responsible for its own removal (and relavent Runtime event listener) would be the best way to achieve this.

Any ideas?

Removing a display object should remove its listener. If you really do need to cycle through hundreds of objects every frame I would suggest managing the list yourself and having only one listener.

Thanks @horacebury, But this object that contains the event listeners is Runtime, since the event listeners are “enterFrame” listeners.

Is there any way to access Runtime’s Listeners?

Those of you looking for an answer here may find this post interesting.

http://www.ludicroussoftware.com/blog/2011/08/24/remove-all-listeners/

However, it’s advised to heed his warnings that might have an affect on how the code is read in future iterations of Corona.

I wonder could any Corona staff clarify on the safety of removing listeners in this way?
 

Your object should have its own destroy function and that function should deal with the runtime removal. That way your caller doesn’t have to worry about it. In fact if you read this past Tuesdays blog post, it talks about overriding built in functions. You could make your own removeSelf that removes the runtime listener then it would call the factory removeSelf.

Ah! I had actually written my own custom removeMe() function in the object’s module and removed the runtime listener within that, but didn’t know how to override corona’s default call to my new custom function.

Thanks Rob, I’ll take another look at the blog post and activate my removeMe() function.

Right, I’m sorry; When I replied earlier I was extremely tired. I should make more sense now.

That said, I still think I was correct in both statements - they just weren’t as linked as I thought because you’re talking about Runtime listeners which would not be removed when a display object is removed from the system.

Moving on, Rob is exactly right in that you can easily override the removeSelf function to remove your objects’ own Runtime listeners.

It’s just IMHO though that you would be better served to add all similar objects to the same display group and have a single Runtime enterFrame listener loop over them. This has the benefit of more easily managed objects and not needing to remove the listeners for each display object; You simply remove the listener at the end of the scene.

@horacebury I do understand your point and I actually have neatly contained similar objects into display groups, but there is still a need for me to create individual modules of objects as there are different types within the over all game.

Having created my own removeSelf() function within the object, when called directly this function works fine, but when removing objects by removing their parent display group, the individual objects’ removeSelf() function is not actually called it seems.

Are there any other suggestions on this method? I am trying to keep the code as clean an easily understood for beginners as possible and to have the object be responsible for its own removal (and relavent Runtime event listener) would be the best way to achieve this.

Any ideas?