Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alpha changing incorrectly #219

Open
mouseas opened this issue Jan 17, 2012 · 1 comment
Open

Alpha changing incorrectly #219

mouseas opened this issue Jan 17, 2012 · 1 comment

Comments

@mouseas
Copy link

mouseas commented Jan 17, 2012

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.
@mouseas
Copy link
Author

mouseas commented Jan 25, 2012

Did a little more poking. My guess is that when the bug does occur, the graphic has alpha applied, and the alpha-applied version is assigned to the graphic's place. Thus each frame the alpha is adjusted, the graphic becomes that much more transparent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant