Handling Z-Index by Screen Coordinates
One problem that I ran into with my game was that my characters and images were not being properly z-indexed. For example, the dwarf in the image below looks like he’s pasted on top of the tree, even though his position on the map suggests he should be behind the tree.
This is because in Corona, objects are z-indexed in the order that they are originally drawn to the screen. The dwarf was originally drawn after the tree, so he will always appear to be in front of the tree, regardless if he’s supposed to be in front or behind the tree.
What I did to fix this is add a couple of functions that run on every frame, loop over all the images, and re-draw the images ordered by their y coordinates.
What happens is that any image positioned further down on the y-axis should appear to be in front of any image that higher up on the y-axis. Since the tree has a lower y coordinate than the dwarf, the tree appears to be in front of the dwarf like this:
Since the indexing functions runs on each frame, objects are correctly indexed as they move across the screen. The dwarf can move from behind the tree to in front of the tree and the objects will be drawn in the correct order to give the perception of depth.