Depth sorting issue

107 views
Skip to first unread message

JustGabe

unread,
Nov 12, 2015, 11:23:58 PM11/12/15
to HaxeFlixel
So, my game looks like this:

So, even for a 2D isometric game, depth sorting is essential, but I haven't been able to figure out how to make a char that was added earlier, to appear over another that was added later and I haven't found a function or something.
The only thing that ocurred to me was to make the usual group of players and enemies, and add a copy of those to a metagroup that will serve to sort them according to their X and Y positions, but after that I'm lost on how to make that happen.

Sam Bellman

unread,
Nov 13, 2015, 4:58:20 AM11/13/15
to HaxeFlixel
Sprites are drawn according to their order in the members array. In order to switch depths you simply need to sort the members array with a sorting function. Flixel comes with some inbuilt ones. Assuming that you have added your sprites to a FlxSpriteGroup rathaer than directly to the state (which is probably best practise) you can simply call
group.sort(FlxSort.byY, FlxSort.ASCENDING);
If you have added them to the state it becomes a little more complicated because the state's members array is <FlxBasic>, and FlxBasics don't have an x or y property.
You could write your own sort function and use that:
sort(mySortByY, FlxSort.ASCENDING);

public static inline function mySortByY(Order:Int, Obj1:Dynamic, Obj2:Dynamic):Int
{
return FlxSort.byValues(Order, Obj1.y, Obj2.y);
}
And if there was another value you wanted to use you would just modify the function to check that instead.
Reply all
Reply to author
Forward
0 new messages