Proper Group Cleaning in Corona (Script Download)

Clean out your display groups efficiently, and with ease. More details and script download here:

http://jonbeebe.tumblr.com/post/3760389212/proper-group-cleaning-in-corona-script-download

UPDATE: The script was recently updated to version 1.1, download it here:

http://cl.ly/58zl

The update includes a check to see if the object has a touch listener attached, and if so, removes it before calling removeSelf() on the display object. [import]uid: 7849 topic_id: 7668 reply_id: 307668[/import]

This looks very very nice, thanks!

I’ll be testing it out later ; ) [import]uid: 33866 topic_id: 7668 reply_id: 27206[/import]

Just gave cleangroup.lua a quick go and seem to only get : attempt to call global ‘cleanGroup’ (a nil value) When using: cleanGroup( localGroup ); localGroup = nil

Is the something I’m doing wrong?
[import]uid: 33866 topic_id: 7668 reply_id: 27213[/import]

@cl-apps: Oops… I accidentally made the cleanGroup() function local in the module.

I updated the download link on my page. Or, you can just go into cleangroup.lua and remove ‘local’ from the cleanGroups function. It’s just that one line that needs modifying, everything else is good.

Sorry about that! [import]uid: 7849 topic_id: 7668 reply_id: 27253[/import]

@jonbeebe - Yep, all works nice now, good work :slight_smile: [import]uid: 33866 topic_id: 7668 reply_id: 27391[/import]

I updated the script to version 1.1 and included something that’s very important, especially for those who use UI buttons in their apps:

Whether you are removing a group by calling group:removeSelf() or by using the cleanGroup() function, version 1.1 will first check to see if there are any touch listeners attached to the object and if so, will automatically remove them.

I tested the script on some ui buttons and it does in fact remove touch listeners before calling removeSelf() on the object.

Personally, I recommend creating a function that will remove all touch listeners, enterFrame events, etc. manually (and then call that before doing any kind of scene changes, etc.), BUT, if you use this cleangroup.lua, it’ll at least provide some added “security”, in case you forget to manually remove one of your touch listeners.

Note, I’m not really aware of any other way to attach a touch listener to an object, but in any case, the functions included in cleangroup.lua will only remove touch listeners that were defined in the following manner:

[blockcode]
myObject.touch = onObjectTouch
myObject:addEventListener( “touch”, myObject )
[/blockcode]

That’s probably how you’re already attaching your touch listeners (that’s also how ui.lua does it, in case you were wondering), and if you do it that way, the cleangroup.lua module will remove an attached touch listener before calling removeSelf() on the display object.

I hope this helps a lot of folks out, I know I’ll be using it in all of my projects moving forward :slight_smile:

Here’s a link to version 1.1 of the script: http://cl.ly/58zl [import]uid: 7849 topic_id: 7668 reply_id: 27418[/import]

I’m finding with recent Corona builds that calling cleangroup() results in a stack overflow due to repeated recursion of calling group.remove. Any ideas how to avoid that?

Investigating further, it seems like when remove() is called on a display group with a nested display group as the argument, when o:removeSelf() is called on the child display group it is just getting called again repeatedly, causing a stack overflow. [import]uid: 43692 topic_id: 7668 reply_id: 123374[/import]

I’m finding with recent Corona builds that calling cleangroup() results in a stack overflow due to repeated recursion of calling group.remove. Any ideas how to avoid that?

Investigating further, it seems like when remove() is called on a display group with a nested display group as the argument, when o:removeSelf() is called on the child display group it is just getting called again repeatedly, causing a stack overflow. [import]uid: 43692 topic_id: 7668 reply_id: 123374[/import]