6 Replies Latest reply on Jun 4, 2007 1:25 PM by kglad

# Changing alpha programatically - alpha values not whole numbers

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?
• ###### 1. Re: Changing alpha programatically - alpha values not whole numbers
Okay, I found part of the answer here:

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?
• ###### 2. Re: Changing alpha programatically - alpha values not whole numbers
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
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

• ###### 3. Re: Changing alpha programatically - alpha values not whole numbers
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
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."

• ###### 4. Re: Changing alpha programatically - alpha values not whole numbers
_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.
• ###### 5. Re: Changing alpha programatically - alpha values not whole numbers
Ahhh...so its just the retrieval of the value that is going to always be a decimal. Thanks, that makes sense.
• ###### 6. Re: Changing alpha programatically - alpha values not whole numbers
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.