Sort my FlxTypedGroup by depth (Child Index)

117 views
Skip to first unread message

Shaun Michael K. Stone

unread,
Jun 20, 2015, 8:36:25 AM6/20/15
to haxef...@googlegroups.com
HELP!


I have a big problem. 

I rely on the members array to arrange the order of how the game objects pass from the right to left.So when I try and arrange the order by Y, it arranges all the game objects from top to bottom. I don't want this. I want the game objects to scatter around the running area, but if a fox is just below an item, the fox should appear above, not underneath. Example: Problem.JPG

I am after this sort of effect:


When I use in my group class:

override public function update():Void 
{
super.update();
this.sort(FlxSort.byY, FlxSort.ASCENDING);
}


I get this effect, not good.

I should be able to set the child index (like a z-index) and not rely on the order of the array!

Thanks,
Shaun.
Problem.JPG

Ohmnivore

unread,
Jun 20, 2015, 11:08:28 AM6/20/15
to haxef...@googlegroups.com
Look into FlxSprite's origin property. The fox isn't actually as high as it's entire sprite in this kind of perspective since you're colliding along the x and y axis and not the third "height" axis. So, what you want to do is make the fox's hitbox correspond to the area of its feet.

Assuming its feet are 10 pixels high on the image:

var feetHeight:Int = 10;
fox.origin.y = fox.height - feetHeight;
fox.height = feetHeight;

Now when flixel sorts the FlxGroup it will display properly since the fox's y value is gonna be lower on the screen than that of the plants.

Brian Flyte

unread,
Jun 20, 2015, 11:53:59 AM6/20/15
to haxef...@googlegroups.com
I'm not sure if this would work in your specific use case, but for my project that has a similar perspective, I found that instead of using FlxSort.byY, which sorts by the top of the hitbox, you can make a custom function to make it sort by the bottom of the hitbox.

I added this function:
private inline function sortByBottomOfY(order:Int, o1:FlxSprite, o2:FlxSprite):Int
   
{
       
return FlxSort.byValues(order, o1.y + o1.height, o2.y + o2.height);
   
}

Then everytime you sort, use
this.sort(sortByBottomOfY);

Sorry if this doesn't relate to your issue!
Reply all
Reply to author
Forward
0 new messages