# Z-order for cocos2d top-down game with sprite batching

I'm trying to understand how to create z-order system like you can see in http://www.realmofthemadgod.com/

Character is above the tree when he is in front of it and vice versa.

Also, I'm planning to use sprite batching and all the sprites will not fit in one texture (2048*2048 for cocos2d-iphone). So I need to use several batches (like one for background and one for characters).

Additional "top-background" layer with above the character layer will not solve the problem: what if two characters are staying in front and behind a tree at the same time?

So, I'm thinking about some sort of masking but I don't know how to properly implement it.

Example from the RotMG: http://i.stack.imgur.com/d8dAe.png

• Do your sprites use any kind of alpha blending? – sam hocevar Dec 6 '11 at 21:28
• Not yet, but probably I'll need it in future. – Ivan Mir Dec 7 '11 at 21:13

You need to implement a "2.5D" system. Taking the picture as reference, the ground is drawn first, then all the sprites, using Y as the order. Start from the top (y=0), bush, bush, tree, plant, tree, character, tree, stick, and so on. Please note the character has been drawn before the tree near him, that's it. TL;DR: Draw sprites according to their Y offset.

It's simple

z_order = position_y


Where axis origin is top left corner of screen(in cocos2d, it's bottom left). Player has origin at right between his foot. tree has origin at it's root.....

If the map has size of 100x100, and player are at map's limit, his z_order must be 100.

If 2 character are at the same y position, their z_order are equal too.