How to sort a FlxSpriteGroup based on a value?

71 views
Skip to first unread message

JustGabe

unread,
Nov 17, 2015, 9:36:58 PM11/17/15
to HaxeFlixel
So, by using
sortingGroup.sort(FlxSort.byY);
All sprites are supposed to get sorted automatically. But the problem is that in my game there are sprites that are several pixels higher than others, for example: if a char is in front of a bush and the bush is smaller, it will look like the bush is over the player when that shouldn't be the case.
My solution was to give each class (player, enemy, overlay tile, etc.) a value that would be equal to this.y + this.height, this way, the char would have a lower value than the bush and so would stay in the front. But I have NO IDEA how to use the
FlxSort.byValues
I've tried to search for examples but found nothing, and I'm clueless as how to use or fill this:
FlxSort.byValues(Order:Int, Value1:Float, Value2:Float) : Int

Thanks for your help

Gama11

unread,
Nov 18, 2015, 4:54:30 AM11/18/15
to HaxeFlixel
byY() is implemented using byValues(). Your sorting function could look something like this: 

public static inline function sortByYPlusHeight(Order:Int, Obj1:FlxObject, Obj2:FlxObject):Int
{
return FlxSort.byValues(Order, Obj1.y + Obj1.Height, Obj2.y + Obj1.Height);
}

sortingGroup.sort(sortByYPlusHeight);
Reply all
Reply to author
Forward
0 new messages