Skip to content

Alpha changing incorrectly #13

Closed
Closed
@FlixelCommunityBot

Description

@FlixelCommunityBot

Issue #219 by: mouseas

I have the following code in a FlxSprite:

override public function update():void {
    // Here is my +1 error, the last time this branch is true, it sets the frame one past the end of the spritesheet.
    if (frame < frames) {  
        frame = frame + 1;
        trace(frame);
    }

    lifespan += FlxG.elapsed;

    if (lifespan > 3) { // 3 seconds before starting fade
        alpha = (1 - ((lifespan - 3) / 15)); // <--- this line...fade slowly over 15 seconds
    }

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

Instead of taking 15 seconds to fade out, the sprite fades out in less than a second. I took a quick poke into the alpha-related code in FlxSprite, and it looks like alpha is adjusted in discrete increments. Problem is, if the amount the alpha is supposed to change is too small, it still drops it one increment. The end result? The FlxSprite is invisible when its alpha value is actually at around 0.95 - 95% opaque, i.e. ever-so-slightly see-through.

Edit: I was able to isolate the problem quite a bit. It was trying to fade on a non-existant frame (thanks to a +1 error on my part), ie the frame it was currently on was a copy of the last frame. I tried using makeGraphic() and the same bug happened. Basically, this bug occurs when the graphic is unique and there's no sprite/spritesheet to reference back to. If the frame is a valid image on a spritesheet, this bug does not occur.

Expected behavior: Sprite fades slowly, based on alpha value.

Actual behavior: If sprite graphic is unique, it fades quickly. The change in actual transparency is not in line with the alpha value.

To reproduce:

  1. Create a sprite and either (a) use makeGraphic() and possibly some draw methods to create a unique graphic, or (b) load an animated spritesheet and set the frame outside the range of existing frames.
  2. Adjust the alpha of the sprite slowly over many frames. Display the numeric alpha value somewhere for comparison; the alpha value should be much higher than the sprite's visibility.
  3. For comparison, use an existing frame or the default image, and adjust the alpha slowly as before. This time the alpha value matches the sprite's visibility.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions