Remove All Objects from a Group

Hello!

Just trying to figure out that I needed to reset one of my scenes (when a level is restarted) and was wondering how to clear all objects from the scene group without actually removing the group itself. Couldn’t find anything regarding this on the documentation so thought I’d ask here.

Thanks in advance

Hi, if you are talking about a displaygroup I am pretty sure you can loop through the displaygroup children and do it.

Other than that, using composer helps when dealing with different rooms/levels.

Anaqim

Remember to loop backwards

Like this

for i = group.numChildren, 1, -1 do   group[i]:removeSelf()   group[1] = nil end

Ah thanks. Wasn’t sure whether or not you could iterate over it like that but makes sense

I would simply use a group or groups to handle this and make them children of the scene group
 

  1. Make a local a the top of your scene file called contentGroup

    local contentGroup

  2. In create() method, create the group and insert it into scenegroup::

    contentGroup = display.newGroup() self.view:insert(contentGroup)

  3. Add all of your objects to ‘contentGroup’

    diplay.newCircle( contentGroup, 10, 10, 10) …

  4. When you want to destroy the objects:

    display.remove( contentGroup ) contentGroup = nil

PS - I strongly advice against using removeSelf().  It is perfectly suitable if you know exactly what you’re doing and you know for sure you made no mistakes, but if you make even a single mistake and call it twice on an object that has already been removed, you will crash your game.
 
So, instead of  this:

obj:removeSelf() 

I suggest always doing this:

display.remove( obj ) -- If obj is invalid, this safely does nothing

Before anyone says it, I know that a direct call to removeSelf() is ever so slightly faster, but that is not a valid reason to bypass the much safer call to ‘display.remove(obj)’.

If you’ are doing so many removals that the alternative safe remove is hurting you, you’ve got other problems already.

Hi roaminggamer,

I have seen that you often promote the use of display.remove(obj) instead of obj:removeSelf() but never taken the time to look closer at it.

But after reading your post here it hit me that

display.remove(obj)

is easier than what I find myself using quite a bit:

if obj~=nil then obj:removeSelf() end

Will be used from now on :slight_smile:

Anaqim

@Anaqim,

Be aware, that even if the object reference is not nil, obj:removeSelf() can fail.  i.e. It may still be invalid.

This is why I so prefer display.remove( obj ).  It is a tiny bit slower, but completely safe.

Sounds great!

Already “converted” my current app to use this display.remove(obj) instead.

Thx  :slight_smile:

Hi, if you are talking about a displaygroup I am pretty sure you can loop through the displaygroup children and do it.

Other than that, using composer helps when dealing with different rooms/levels.

Anaqim

Remember to loop backwards

Like this

for i = group.numChildren, 1, -1 do   group[i]:removeSelf()   group[1] = nil end

Ah thanks. Wasn’t sure whether or not you could iterate over it like that but makes sense

I would simply use a group or groups to handle this and make them children of the scene group
 

  1. Make a local a the top of your scene file called contentGroup

    local contentGroup

  2. In create() method, create the group and insert it into scenegroup::

    contentGroup = display.newGroup() self.view:insert(contentGroup)

  3. Add all of your objects to ‘contentGroup’

    diplay.newCircle( contentGroup, 10, 10, 10) …

  4. When you want to destroy the objects:

    display.remove( contentGroup ) contentGroup = nil

PS - I strongly advice against using removeSelf().  It is perfectly suitable if you know exactly what you’re doing and you know for sure you made no mistakes, but if you make even a single mistake and call it twice on an object that has already been removed, you will crash your game.
 
So, instead of  this:

obj:removeSelf() 

I suggest always doing this:

display.remove( obj ) -- If obj is invalid, this safely does nothing

Before anyone says it, I know that a direct call to removeSelf() is ever so slightly faster, but that is not a valid reason to bypass the much safer call to ‘display.remove(obj)’.

If you’ are doing so many removals that the alternative safe remove is hurting you, you’ve got other problems already.

Hi roaminggamer,

I have seen that you often promote the use of display.remove(obj) instead of obj:removeSelf() but never taken the time to look closer at it.

But after reading your post here it hit me that

display.remove(obj)

is easier than what I find myself using quite a bit:

if obj~=nil then obj:removeSelf() end

Will be used from now on :slight_smile:

Anaqim

@Anaqim,

Be aware, that even if the object reference is not nil, obj:removeSelf() can fail.  i.e. It may still be invalid.

This is why I so prefer display.remove( obj ).  It is a tiny bit slower, but completely safe.

Sounds great!

Already “converted” my current app to use this display.remove(obj) instead.

Thx  :slight_smile: