I have descided to Create an object (class) to hold each sprite. I then put those objects into a table.
The class has all the sprite characteristics plus more.
One characteristic is “layer”. If any object should be ontop or behind another object.
In general it seems the last item drawn is on top.
So all objects are in my controls table. Any easy way to sort a table by class property? I figure once sorted I can the draw in order.
I also anticipate adding objects on the fly so the sort needs to be quick. Perhaps the better approach is to use insert(t,p,v) after I figure it’s place?
You could look into the zLevel(z) function. This will let you set the ordering of the sprite when rendered under the default orthographic projection. (It just transforms the Z coordinate of the model matrix.)
Alternatively, you can sort your table (
items) by doing this (assuming it is full of your object type, which has a
return a.layer < b.layer
So that works! Great.
Now a new issue.
For draw I need the last one drawn to be on top. For touch I need it to be the first one.
I tired to copy the table like this
t2 = t
It appear = is a pointer
So does t2 = t.clone() exist?
Obviously I can just maintain two separate tables but that seems silly. I don’t want to change the sort on the fly, that might cause performance issues later.
No, there is no built in clone/copy function for tables, though it is pretty straightforward to write a shallow copy. If you need a deep copy, then it’s up to you to figure out exactly how a deep copy should be implemented for your case, because not everbody needs the same thing from a deep copy.
How about a for loop with step -1 ?
Yeah, if you maintained your sorting for drawing, and iterated backwards for touching, that should work.
The joys of Lua.
Not to bad… For 10,1 doesn’t seem to work so I went wit do while…
i = table.maxn(controls)
while i > 0 do
c = controls[i]
if touchObject(t,c) then
To iterate backwards with a for loop you need to set the increment (it defaults to +1)
for i = 10,1,-1 do