Originally Posted by
Konidias
Glad you asked! :) The reason behind this is so that you can assign "layers" to each object while also allowing them to sort by their Y position. Since we are sorting by descending, that means the bottom most objects will draw to the front. We would run into lots of layering issues if we just let it sort the objects only by the Y position...
For example, if you have a Background object at Y position 300 and a Foreground object at Y position 295, then the Background object would be drawing in front of the Foreground object. (which we don't want)
But if we use the alterable value for the "layer" (foreground being 2) and multiply that by the bottom most Y position, then it effectively sorts all objects by their "layer" category. Basically the method I posted allows you to sort by the value of the layer number you set, PLUS the y position of the objects.
If you just sort by the value (and not the Y position) then you're going to get sort issues with the same layer objects drawing oddly on each other. (like 2 foreground objects not sorting to the proper layers, since they are both just told to draw above the other layers, but not sort themselves out)
What is happening is if you have a window that is 480 pixels high:
Objects with value 0 are sorted by their y positions from 0 to 480.
Objects with value 2 are sorted by their y positions from 0 to 480 PLUS 2*480. This ensures that objects marked with value 2 will ALWAYS sort over objects at value 0, since the final range is 960 to 1440.
So the layer object sees all these value 0 objects and sorts them from 0 to 480, and then it sees all these value 2 objects and sorts them from 960 to 1440.
Since 960 to 1440 is greater than 0 to 480, it will ALWAYS draw any value 2 object above value 0 objects... Even if the value 2 object's y position is much lower.