flixel icon indicating copy to clipboard operation
flixel copied to clipboard

Patch Sprite Color Batching and Default Sprite Color

Open RapperGF opened this issue 7 months ago • 2 comments

Currently, in HaxeFlixel, attempting to batch multiple FlxSprites with color transforms causes batching to break. If any sprite has a color that is not pure white, it will batch. This causes sprites to be drawn in separate colored batches, even if the color is set.

This PR changes the default color of sprites to FlxColor.TRANSPARENT (effectively zero tinting) while still respecting all colors, including FlxColor.WHITE

With this change:

  • Sprites set to FlxColor.WHITE will be batched together without altering their original colors.
  • Sprites without a color (or with FlxColor.TRANSPARENT) will render with no tint, restoring previous behavior.

Previous Behavior:

image

New Behavior:

image

Example project:

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.util.FlxColor;
import flixel.group.FlxSpriteGroup;

class ColoringTest extends flixel.FlxState {

    var grp:FlxSpriteGroup;

    override public function create() {
        grp = new FlxSpriteGroup();

        var sprW = 16;
        var sprScale = 5;
        var spacing = sprW * sprScale;

        var screenW = FlxG.width; 
        var spritesPerRow = Math.floor(screenW / spacing);

        var totalSprites = 60;

        for (i in 0...totalSprites) {
            var row = Math.floor(i / spritesPerRow);
            var col = i % spritesPerRow;

            var spr = new FlxSprite(col * spacing, 20 + row * spacing);
            spr.scale.set(sprScale, sprScale);

            if (i == 0) {
                // First sprite is always white
                spr.color = FlxColor.WHITE;
            } else {
                // 20% chance to be white, otherwise random color
                if (Math.random() < 0.2) {
                    spr.color = FlxColor.WHITE;
                } else {
                    spr.color = FlxColor.fromRGB(
                        Std.int(Math.random() * 256),
                        Std.int(Math.random() * 256),
                        Std.int(Math.random() * 256)
                    );
                }
            }

            grp.add(spr);
        }

        grp.screenCenter();
        add(grp);
    }
}

RapperGF avatar Sep 28 '25 18:09 RapperGF

fire 🔥

LeonGamerPS1 avatar Sep 28 '25 18:09 LeonGamerPS1

Moving off of the 6.1.2 release. changing the default color is a breaking change and a somewhat hacky fix, imo

Geokureli avatar Nov 11 '25 18:11 Geokureli

Wouldn't this potentially break alpha related stuffs when a sprite is black?

rodney528 avatar Dec 12 '25 10:12 rodney528