Marsh
Marsh

Reputation: 8145

Flixel - Alpha not working properly

I have something odd happening when I update a FlxSprite's alpha repeatedly to make it fade out. Instead of taking 150 seconds to fade out completely (I actually want 15 seconds), it fades out over maybe 2 seconds. I tried tracing the actual alpha value, and the sprite is invisible when the alpha value is around 0.95 (95% opacity, ie slightly transparent).

Does anyone know how I can get the alpha to work properly in Flixel?

override public function update():void {
    lifespan += FlxG.elapsed;

    if (lifespan > 3) {
        alpha = (1 - ((lifespan - 3) / 150)); // <--- this line

    }

    if (alpha < 0.01) {
        State.s.remove(this, true);
    }
    super.update();     
}

Upvotes: 2

Views: 551

Answers (2)

Marsh
Marsh

Reputation: 8145

There's a bug in Flixel currently. If an animated sprite is used and the current frame is outside the spritesheet's range, the alpha doesn't work quite right. The same happens if makeGraphic() is used.

This bug has been logged on the Flixel github issue list, and hopefully it will be fixed in the next version.

Upvotes: 1

WgFunstorm
WgFunstorm

Reputation: 148

I copy-pasted your code in an empty Flixel 2.55 project and it worked exactly as expected. Are you sure there isn't anything else in your project interfering with the lifespan variable or sprite's alpha? Try it with a fresh project and see if you still run into the same issue.

Just a tip, you can use FlxG.state to reference the current state at all times, no need to store a separate reference. Also alpha is automatically clamped to 0,1 so you can test for 'if (alpha == 0)' without worrying about your alpha value going into negatives.

Upvotes: 1

Related Questions