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]