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 call sortSpriteByZ() before or after super.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:

    Imgur



  • 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()to add(Object:FlxBasic) just doesn't work.

    Why should overlapping objects have different Zs? Just 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 same Z value.

    Still wanting to know where I should put sortSpritesByZ() instead of update() 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 call add. This is working now (I was calling sortSpritesByZ() before super.add(Object) and not after ), but there's still the issue of this not updating if I change a sprite's Z. Since the property comes from an Interface, I can't really have a set 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 SortableFlxSpriteor 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.


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.