Object position question

Hi i dont have a good programming structure, im trying to learn lua as i try stuff and read documents and tutorials.
I’ve managed to make something, but i have a lot of redundant code. Thats not really the problem since im not trying to master my programming skill for my first game, but i usually get stuck on some parts. Actually im stuck in something related to objects position on the screen. I wont post the entire code because it has more than 1000 lines (like i said, very primitive and redundant but its ok for me) but i can reproduce it:

I have 3 groups (im not talking about the display.newGroups, im using tables) of objets, each of them has 3 images, and im using 3 differents buttons to render each of the group images. For example:

group01 = head,ear,nose
group02 = hand,chest,arm
group03 = leg, foot, shoes

Every time i press group01_button, it renders head, the next time ear, etc (using tables), same for 02 and 03.

How do i do to show group01’s objets allways above group02 and group03? I know about toFront and toBack functions, but i have a lot of groups (and other stuff like background and maybe particles) and it’d be a mess.
Some advice? How’d you do it?

Thanks in advance! [import]uid: 108461 topic_id: 23627 reply_id: 323627[/import]

hi undecode,
just very quickly, i would have two suggestions:
first, as you probably know, the display order of the objects >>inside the same group<< depends on the order they are inserted to the group.
so if you have:

groupPlayer:insert ( head )  
groupPlayer:insert ( nose )  

nose will always draw over head, similar to the effect to toFront and toBack ( I assume, as I have never used toFront or toBack )

another suggestion is to organize your scene in nested groups.
its good to have one top-level group that will contain several other groups which will maintain the same display order relations.
for example, I will sketch hierarchy here:

topGroup  
 L Background1Group ( the furthest behind, ie. sky)  
 L Background2Group ( closer to player, ie. hills or buildings )  
 L EnemiesGroup ( ie. if you want to draw them behind the player)  
 L PlayerGroup  
 L head  
 L nose  
 L ear  
 L body  
 L PartclesGroup (if you want to draw them in front of player)  
 L HUDDisplayGroup ( score text, pause button, or similar )  

The order I’ve written these groups is the order the groups need to be inserted into the main topGroup.

so

topGroup:insert ( background1Group)  
topGroup:insert ( background2Group)  

means that background2Group will always draw above background1Group

it’s a good practice to create all the main groups at the beginning of level, and then only manage objects inside of groups, and not reorder the main groups again.
if because of your particular game logic you need to add another group (or object) behind already existing one, you can specify index of appearance, so for example:

topGroup:insert ( 2, moon )  

will now insert moon object behind bacground2Group but in front of background1Group

hope its not too confusing, hehe :slight_smile: [import]uid: 80100 topic_id: 23627 reply_id: 94821[/import]