Displaygroup hierarchy

I’m struggling with display-groups.
Basically i want a background in the “back”, an animated sprite from a sprite sheet in the “middle” and buttons in the “front”
See image below:
http://imgur.com/36Cbv

Currently i have 3 functions with 3 different display-groups within, this only shows the background.
I don’t want all the code in one function because that would be really messy to work with.

Is it possible to use the same display-group in 3 different functions and give the objects hierarchy order using
[lua]assert( (group[1] == button) and (group[2] == sprite) and (group[3] == background) )[/lua]

Any help is appreciated :slight_smile: [import]uid: 129637 topic_id: 22893 reply_id: 322893[/import]

There are several ways you can accomplish this. The easiest being :

local layer1Group = display.newGroup()  
local layer2Group = display.newGroup()  
local layer3Group = display.newGroup()  

If you create the groups in a row like that then the z order is already defined for you.

Layer1 is the first group, layer 2 is the group that will display above that, and layer 3 displays above layer 2.

So you insert the background into layer 1, sprite into layer2 , and buttons into layer 3.

Or alternatively :

local localGroup = display.newGroup()  
  
--Insert your items in needed order  
localGroup:insert(background)  
localGroup:insert(sprite)  
localGroup:insert(buttons)  
  

That way will also display them in the order you described. It all comes down to personal preference as to which way you go about this. Option 1 is the easiest as you don’t have to worry about inserting your items in order. [import]uid: 84637 topic_id: 22893 reply_id: 91470[/import]