Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Understanding Parent optional parameter
Started by J@V0 Feb 19 2019 11:46 AM

2 replies to this topic
parent

Best Answer XeduR @Spyric , 19 February 2019 - 12:09 PM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

J@V0

[GLOBAL: userInfoPane.html]
J@V0
  • Enthusiast

  • 43 posts
  • Corona SDK

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) ?



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 687 posts
  • Corona SDK

  Best Answer

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



[TOPIC: post.html]
#3

J@V0

[GLOBAL: userInfoPane.html]
J@V0
  • Enthusiast

  • 43 posts
  • Corona SDK

Oh great! The thing didn't know was "The end result is exactly the same". Thanks!




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: parent