Properly remove display group module

If I create a module with the syntax

object.lua:

local object = display.newGroup()  
view:insert(object)  
return object  

main.lua:

require("storyboard")  
require("object")  
require("some other object")  

I am on a scene, and I want to remove all display objects, and reintroduce new assets.

What is the best way of doing this?

I can purgeScene to remove all display objects, and then gotoScene again to start the createScene again. I set this up quickly as it was easy, but it doesn’t feel like the right solution.

Another way that seems more like the right way is to create object.removeSelf() that listens to a custom event and removes event listeners & display objects from the display group. This sounds like the “best” way but I am wondering if there is an easier way to go about this. It seems purgeScene removes all display objects and seems like an easy way to do it, I don’t think it removes event listeners though and I am not sure how efficient this is purging and recreating the scene over and over.
[import]uid: 160288 topic_id: 33624 reply_id: 333624[/import]

Well first, when you destroy a display object it automatically removes all event listeners attached to that object. This is true whether you invoke removeSelf() manually or you use the storyboard to clear the display objects.

If all you need to do is get rid of the old objects and introduce new ones you can easily iterate through your display group and remove all child objects like so:

[lua]while myGroup.numChildren > 0 do
myGroup[1]:removeSelf();
end[/lua]

and then introduce your new objects. I can’t recall if removing a group removes all child objects, but if it does then you could alternatively just myGroup:removeSelf().
If in your createScene event you have a lot of extra logic that needs to be run through again then IMO you would be better off just switching scenes. Switching scenes is super fast, it’s all the code/assets loading on the create/enter scene events that would make it inefficient. [import]uid: 147305 topic_id: 33624 reply_id: 133681[/import]

Well first, when you destroy a display object it automatically removes all event listeners attached to that object. This is true whether you invoke removeSelf() manually or you use the storyboard to clear the display objects.

If all you need to do is get rid of the old objects and introduce new ones you can easily iterate through your display group and remove all child objects like so:

[lua]while myGroup.numChildren > 0 do
myGroup[1]:removeSelf();
end[/lua]

and then introduce your new objects. I can’t recall if removing a group removes all child objects, but if it does then you could alternatively just myGroup:removeSelf().
If in your createScene event you have a lot of extra logic that needs to be run through again then IMO you would be better off just switching scenes. Switching scenes is super fast, it’s all the code/assets loading on the create/enter scene events that would make it inefficient. [import]uid: 147305 topic_id: 33624 reply_id: 133681[/import]