Object destruction question...

I wonder how to remove objects when they are created like this for example:

enemy1=display.newImageRect(…)

enemy1.wall=display.newRect(…)

enemy1.wall2=display.newRect(…)

enemy1.collisionRadius=display.Circle(…)

is it “enough” to destroy enemy1 to also remove the other display objects?

Hi Daniela,

No, you can’t just destroy the “parent” object and expect the “child” objects to be destroyed as well… at least not in this usage case (if you created a parent display group, you could remove the group and all children would die with it).

It may be better to relate these children to the parent in a more elegant way. Consider creating a table as a property of the parent, then adding children to the table. Later, you could loop through that table and destroy the children, followed up by destroying the parent.

Of course there are other options and ways to handle this… this is just one suggestion.

Take care,

Brent

The below blogpost is kind of old, but the theory discussed is still sound:

https://coronalabs.com/blog/2012/10/09/dynamically-optimized-sprite-sheets/

The nice thing about this approach is that you can, as Brent states, elegantly loop through the objects within the “parent” display group and remove them.

Thank you for the info!

Hi Daniela,

No, you can’t just destroy the “parent” object and expect the “child” objects to be destroyed as well… at least not in this usage case (if you created a parent display group, you could remove the group and all children would die with it).

It may be better to relate these children to the parent in a more elegant way. Consider creating a table as a property of the parent, then adding children to the table. Later, you could loop through that table and destroy the children, followed up by destroying the parent.

Of course there are other options and ways to handle this… this is just one suggestion.

Take care,

Brent

The below blogpost is kind of old, but the theory discussed is still sound:

https://coronalabs.com/blog/2012/10/09/dynamically-optimized-sprite-sheets/

The nice thing about this approach is that you can, as Brent states, elegantly loop through the objects within the “parent” display group and remove them.

Thank you for the info!