I would still dump all objects that need to be drawn into a single table, then sort that table by its y values. (With higher Y's coming later than lower Y's. So that lower Y's get drawn first.)
A sample sorting algorithm:
Code: Select all
function sort(T)
table.sort(T,
function(first, second)
return first.var1 < second.var1
end
)
end
Where var1 would be the table column to sort by. (In this case, the Y) And T is the table name. i.e. sort(tablename)
So, basically, every frame you need to:
1) Reset a table whose one reason for existing is to hold all the objects by calling tablename = {}.
2) Add all enemies to a table -- table.insert(tablename, {x = whatever, y = whatever, etc...}
3) Add all other stuff to the same table
4) Add the player(s) to the same table -- table.insert(tablename, { x = playerx, y = playery, etc...}
5) Sort the table -- sort(tablename)
6) run through the table using for i,t in ipairs(tablename) to draw all the objects
Should work swimmingly.