Understanding Parent optional parameter

I’m trying to understand the use of the Parent optional parameter used for example in display.newText

The documentation says:

parent (optional)

GroupObject. Display group in which to insert the text object.

 

But when should I use it?

Is it recommended to always use it?

What are the benefits of stating the parent display group?

 

Could you provide an example where it is useful (and required) ?

Normally, you want to place any and all display objects into some display group. This makes controlling and removing them in scene changes easy.

There are two ways of inserting display objects into groups. Either by using the optional parameter that you mentioned or by using a specific function call, i.e.

 

local myGroup = display.newGroup() local object1 = display.newRect( myGroup , 240, 160, 200, 40 ) local object2 = display.newRect( 240, 160, 200, 40 ) myGroup:insert( object2 )

In the code above, I insert both object1 and object2 into myGroup. The end result is exactly the same. If you know which group you want to insert the object into as you create them, then using the optional parameter is perhaps the faster and cleaner approach.

Edit: Oh, and you should also read https://docs.coronalabs.com/guide/graphics/group.html

Oh great! The thing didn’t know was “The end result is exactly the same”. Thanks!

Normally, you want to place any and all display objects into some display group. This makes controlling and removing them in scene changes easy.

There are two ways of inserting display objects into groups. Either by using the optional parameter that you mentioned or by using a specific function call, i.e.

 

local myGroup = display.newGroup() local object1 = display.newRect( myGroup , 240, 160, 200, 40 ) local object2 = display.newRect( 240, 160, 200, 40 ) myGroup:insert( object2 )

In the code above, I insert both object1 and object2 into myGroup. The end result is exactly the same. If you know which group you want to insert the object into as you create them, then using the optional parameter is perhaps the faster and cleaner approach.

Edit: Oh, and you should also read https://docs.coronalabs.com/guide/graphics/group.html

Oh great! The thing didn’t know was “The end result is exactly the same”. Thanks!