mini code-tutorial on Groups

I thought it may be useful to others too…

[lua]–[[
Turn on-off the commented code lines in the order they appear,
relaunch the project and watch the screen and the terminal output

–//this is a comment
–this = code.turnoff(true)

]]

local red = display.newRect( 20, 0, 100, 100 ) --// Red square is at the bottom
red:setFillColor( 255, 0, 0 )
local green = display.newCircle( 80, 120, 50 ) --// Green circle is in the middle
green:setFillColor( 0, 255, 0 )
local blue = display.newRect( 0, 50, 100, 100 ) --// Blue rect is at the top
blue:setFillColor( 0, 0, 255 )

local group = red.parent --//the group here is the Stage Object

local mygroup=display.newGroup() --//lets add the shapes to a handmade group
mygroup:insert(red)
mygroup:insert(green)
mygroup:insert(blue)

–//you can bring an object at top (last in mygroup array)
–mygroup:insert(mygroup.numChildren+1, red)

–//or send an object at bottom (first in mygroup array)
–mygroup:insert(1, blue)

myscreen = display.newGroup() --we need one more group…
–//you can also add functions to a group

–//as a method of the group object
function myscreen:compute(a,b)
return a + b
end

–//or as a record in the group table
myscreen.calc = function(a,b)
return a+b
end

–//and then call the functions
print( myscreen:compute(1,1) )
print( myscreen.calc(3,3) )
–//an object can only belong to *one* group
–//when you insert a grouped object into a second group,
–//it is firstly get removed from the first group
myscreen:insert(blue) --//now it belongs no more to mygroup, only to myscreen
myscreen:insert(red)

–//you can always add a group inside another group
myscreen:insert(mygroup)
–//you can remove an element of a group (note: it gets deleted as an object!)
–myscreen:remove(mygroup)
–myscreen:remove(red)
–red:setFillColor( 250, 0, 0 ) --//gives an error, since red doesn’t exist!
–//remove a group, to delete all (and only) the *display* objects it has inside
–myscreen:removeSelf()
–print( myscreen:compute(7,7) ) --//still there!

–//nulify the group to delete all (and only) the *data* objects it holds
–//(non-integer indexed vars, such as functions, tables etc you have stored as properties)
–myscreen=nil
–print( myscreen:compute(8,3) ) --//gives error, since myscreen doesn’t exist now
–//but the display objects are still visible (if group not explicitly removed above)
–//because they are still stage objects, even with the holder group got killed
print(type(myscreen))[/lua] [import]uid: 7356 topic_id: 2325 reply_id: 302325[/import]

Thank you Magenda this is great and it would be nice if it were linked from a Concepts or everywhere groups are mentioned.

  • April [import]uid: 10016 topic_id: 2325 reply_id: 8141[/import]

Is the comment block at the top supposed to do something? My understanding of the text is that it should possibly output comment content to the console, but I see nothing different when running it with different values. Or am I reading too much into it?

Thanks,

Matt. [import]uid: 8271 topic_id: 2325 reply_id: 16099[/import]

Actually the line:
[lua]–this = code.turnoff(true)[/lua]
…was just a slick/nerdy way to denote the typographical convention I use for lines with code that has been turned off (and can be turned on by removing the comment symbols).

You are supposed to comment out or re-activate this kind of lines (like lines 25, 28 etc) to see what is the resulting effect on groups and objects inside them.

In other words, this mini-tutorial is just a ready-to-play-with testbed, to help you understand how Groups work in Corona.

[import]uid: 7356 topic_id: 2325 reply_id: 16106[/import]

[lua]true[/lua] [import]uid: 8271 topic_id: 2325 reply_id: 16108[/import]

;p

[import]uid: 7356 topic_id: 2325 reply_id: 16122[/import]