2

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();     
}
Mar
  • 7,765
  • 9
  • 48
  • 82

2 Answers2

1

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.

WgFunstorm
  • 148
  • 1
  • 10
  • I'll give a clean project a try, but the project is pretty simple, and I hadn't been messing with alpha prior, other than my sprite having a portion of ~50% opacity pixels. – Mar Jan 19 '12 at 15:03
  • I left out the part that runs it through the frames of its animation when it's first started. There's a +1 error that tells it to load a frame one past the end of the spritesheet. When I fixed that error, the problem was solved. I also tried `makeGraphic()`, and it looks like unique bitmaps exhibit this error while reusable ones do not. I'll log it in Flixel's bug list. – Mar Jan 25 '12 at 16:30
1

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.

Mar
  • 7,765
  • 9
  • 48
  • 82
  • Good news! This bug has been fixed in the Community version of Flixel v2.56: https://github.com/FlixelCommunity/flixel – IQAndreas Nov 28 '13 at 06:16