Sort "flickers" on Flash target
-
Hello! :wave:
I'm trying to implement a
Z
variable system to allow for easy sprite sorting. My current code works fine on Windows, but the sprites flicker on Flash, regardless of whether I callsortSpriteByZ()
before or aftersuper.update(elapsed)
!Code:
function sortSpritesByZ() { sort(function(Order:Int, entityA:FlxBasic, entityB:FlxBasic):Int { var zA:Int = 0; var zB:Int = 0; if (Std.is(entityA, IZSortable)) zA = cast(entityA, IZSortable).Z; if (Std.is(entityB, IZSortable)) zB = cast(entityB, IZSortable).Z; return FlxSort.byValues(Order, zA, zB); }, FlxSort.ASCENDING); }
And my
IZSortableInterface
:interface IZSortable { public var Z:Int; }
Result on Flash:
-
You should re-sort only when in group object added or changed its Z. Also make sure that overlapping objects have different Z.
-
Moving my
sortSpritesByZ()
toadd(Object:FlxBasic)
just doesn't work.Why should overlapping objects have differentJust found out that's precisely why the sprites flicker; I guess HaxeFlixel isn't deterministic on how it decides which sprite should be on top, if they have the sameZ
s?Z
value.Still wanting to know where I should put
sortSpritesByZ()
instead ofupdate()
to prevent my FPS from plummeting.
-
What do you mean by "moving my sortSpritesByZ() to add(Object:FlxBasic)"? Just call sortSpritesByZ() at the point where you add objects to FlxGroup (after both setting Z and adding to be precise).
-
@starry-abyss I mean
override public function add(Object:FlxBasic):FlxBasic { super.add(Object); sortSpritesByZ(); return Object; }
Which is meant to be cleaner than having to remember to call
sort
every time I calladd
. This is working now (I was callingsortSpritesByZ()
beforesuper.add(Object)
and not after ), but there's still the issue of this not updating if I change a sprite'sZ
. Since the property comes from an Interface, I can't really have aset
behaviour. Anyone have any ideas?(By the way; flicker can be solved using
function sortSpritesByZ() { sort(function(Order:Int, entityA:FlxBasic, entityB:FlxBasic):Int { var zA:Int = 0; var zB:Int = 0; if (Std.is(entityA, IZSortable)) zA = cast(entityA, IZSortable).Z; if (Std.is(entityB, IZSortable)) zB = cast(entityB, IZSortable).Z; if (zA == zB) zB++; return FlxSort.byValues(Order, zA, zB); }, FlxSort.ASCENDING); }
)
-
Can't you create similar convenience function, but for setting Z?
-
@starry-abyss From within the sprite itself, only if I have the sprite extend a
SortableFlxSprite
or something, but I was trying to avoid that, since I can't then have the sprite extend anything else...
-
I mean just have a method in PlayState or wherever which changes Z and calls sort.