Skip to content

Incorrect coloring of animated sprites with useFramePixels on renderTile #3414

@richTrash21

Description

@richTrash21

While testing #3413 i've encountered an issue where using useFramePixels on animated sprites would incorrectly apply color transformation. This bug affects only renderTile targets.

Here's code to reproduce this issue:

package;

import flixel.FlxSprite;
import flixel.text.FlxText;

class TestState extends flixel.FlxState
{
	var colors = [0xAA0000, 0x00AA00, 0x0000AA, 0xAAAA00, 0x00AAAA];
	var curColor = 0;

	var bugged:FlxSprite;
	var expected:FlxSprite;

	override public function create()
	{
		super.create();

		FlxG.camera.bgColor = 0xFF999999;

		add(new FlxText(50, 20, 0, "Bugged", 16));

		bugged = makeCube(30, 60);
		add(bugged);
		bugged.useFramePixels = true;

		add(new FlxText(200, 20, 0, "Expected", 16));

		expected = makeCube(180, 60);
		add(expected);

		add(new FlxText(350, 20, 0, "Graphic", 16));

		final graphic = makeCube(330, 60);
		add(graphic);

		switchColor();
		new flixel.util.FlxTimer().start(3, switchColor, 0);
	}

	override public function update(elapsed:Float)
	{
		super.update(elapsed);

		if (FlxG.keys.justPressed.Q)
			changeAlpha(-1);
		if (FlxG.keys.justPressed.E)
			changeAlpha(1);
	}

	function makeCube(x:Float, y:Float):FlxSprite
	{
		// can be just solid color sliced into frames
		final cube = new FlxSprite(x, y).loadGraphic("assets/images/cube_test.png", true, 70, 70);
		cube.setGraphicSize(120, 120);
		cube.updateHitbox();
		cube.animation.add("anim", [0, 1, 2], 8);
		cube.animation.play("anim");
		return cube;
	}

	function switchColor(?_)
	{
		final color = colors[curColor];
		bugged.color = color;
		expected.color = color;
		curColor = ++curColor % colors.length;
	}

	function changeAlpha(mult:Int)
	{
		bugged.alpha += 0.1 * mult;
		expected.alpha += 0.1 * mult;
	}
}

Running this code will result in this: applied color appear darker or as a mix of previous and current colors.

project_axyu_5QWcf0LkDa.mp4

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions