Skip to main content
Participant
June 4, 2007
Question

Changing alpha programatically - alpha values not whole numbers

  • June 4, 2007
  • 6 replies
  • 387 views
I'm doing alpha changes on an image programatically in an onEnterFrame method. My code is this simple:

trace(mc1_mc._alpha);
mc1_mc._alpha -= 2;
trace(mc1_mc._alpha);

The output is:
100
97.65625

My question is, how is 100 - 2 = 97.65625? What is going on here? It gives this answer even if I use Math.round() on it. Any ideas?
This topic has been closed for replies.

6 replies

kglad
Community Expert
Community Expert
June 4, 2007
yes. and flash stores those _alphas as integers from 0 to 256, not 255.

so if you start with an _alpha of 100 (256 to flash), subtract 2 (which is 256/50 rounded up to 6 to flash) you think you have an _alpha of 98 (but flash subtracts 6 from 256 to give an _alpha of 250). if you ask flash for that _alpha, flash returns 250/256*100 = 97.65625.
Participant
June 4, 2007
Ahhh...so its just the retrieval of the value that is going to always be a decimal. Thanks, that makes sense.
kglad
Community Expert
Community Expert
June 4, 2007
_alpha values are stored internal to flash as integers from 0 (full transparency) to 255 (0 transparency).

when you set an _alpha value using actionscript or in the authoring environment you use 0 to 100. flash converts that to an integer from 0 to 255. when you retrieve _alpha from flash it yields an exact decimal representation of the _alpha percentage.
Inspiring
June 4, 2007
crownnation,

> But its still not working. I get this output:
>
> mc1_mc._alpha: 100
> Alpha: 98
> mc1_mc._alpha: 97.65625

You'll always get that. This is a matter of how the inner value is
"translated" into a 0 through 100 equivalent.

> Any ideas on what I'm missing?

You're not missing anything. This is one of those things that simply is
what it is. The extra variable tip, in your amended sample code, allows you
stick with integers while changing alpha continuously -- say, in an
onEnterFrame loop -- which may (or may not) affect processing speed. You
might get a smoother transition.


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Inspiring
June 4, 2007
crownnation,

> My code is this simple:
>
> trace(mc1_mc._alpha);
> mc1_mc._alpha -= 2;
> trace(mc1_mc._alpha);

Yup.

> The output is:
> 100
> 97.65625

Surprising, right?

> My question is, how is 100 - 2 = 97.65625? What is going
> on here?

According to Sham Bhangal, author of Flash Hacks, that's because alpha
is actually measured in 256 units (like color), even though ActionScript
makes it seem like 0 to 100. So there's a bit of fudging in the math.

> It gives this answer even if I use Math.round() on it. Any
> ideas?

Not sure how you're pulling that off. For me, the following outputs 100
and 98:

trace(mc._alpha);
mc._alpha -= 2;
trace(Math.round(mc._alpha));


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Participant
June 4, 2007
Okay, I found part of the answer here:
http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=288&threadid=1229915&highlight_key=y&keyword1=alpha

So now I've tried:
var alpha1 = 100;
trace("mc1_mc._alpha: " + mc1_mc._alpha);
alpha1 -= 2;
trace("Alpha: " + alpha1);
mc1_mc._alpha = alpha1;
trace("mc1_mc._alpha: " + mc1_mc._alpha);

But its still not working. I get this output:

mc1_mc._alpha: 100
Alpha: 98
mc1_mc._alpha: 97.65625

Any ideas on what I'm missing?