Most people using Flash are unaware of the way it handles the alpha property. Many believe that it is a percentage from 0 - 100... which is understandable because this is how Flash allows you to handle it.

Well the truth is, it's not. It's actually converted to a number from 0 - 255. So when you set the _alpha property, Flash takes that value and converts it to it's appropriate percentage in the 0-255 base, the real problem is that the number is converted and rounded internally, which means you are completely unaware of not only the process, but it's results.

Alright so if you don't believe let's run a little test.

Open Flash and start a new file.
Then select Frame 1 and open the Actions Panel, paste the following code into the panel.

showAlpha();

function showAlpha()
{
   this._alpha = 0;
   for (var i = 0; i < 100; i++)
   {
      this._alpha++;
      trace(this._alpha);
   }
}



Take note that we are not worried about the actual fading of the movieclip, we are only concerned with the values. This is the reason we are using a for loop, which would not be practical in a fading scenario.


If you run this code you will find that the last few results look like this..

75
75.78125
76.5625
77.34375
78.125


This really shows how unbelievably innacurate the alpha property is, in our example we end up more than 20% off of what we expected.

This innacuracy is also really bad because we are incrementing it, it wouldn't be so bad if we were manually setting each value, but the problem is that it's still innacurate.

Most percentage values have no corresponding exact value in the 0-255 range, but 5 of them do, they are:

0%, 25%, 50%, 75% and 100%

So how can we fix this innacuracy?

The method I use to make up for this innacuracy is simple, I wrote a class a while back for modifying alpha values for a particular movieclip, the drawback for this class is that their needs to be one instance for each MovieClip, the code for our custom AlphaModifier class is below...

class AlphaModifier
{
   // the alpha property, we store this independent of the movieclips alpha property
   private var alpha:Number;
   // the movieclip that this AlphaModifier is targeting
   private var target:MovieClip

  /**
    * Constructor
    * @param The movieclip that we will be modifying with our AlphaModifier class.
    */

   public function AlphaModifier( target:MovieClip )
   {
      this.target = target;
      alpha = target._alpha;
   }

   /**
     * Returns the alpha for the targeted movieclip.
     */

   public function get _alpha() : Number
   {
      return alpha;
   }

  /**
    * Sets the alpha for the targeted movieclip.
    */

   public function set _alpha( alpha:Number ) : Void
   {
      target._alpha = alpha;
      this.alpha = alpha;
   }
}


Why this works is actually really simple. Our biggest issue with the innacuracies in Flash are the fact that it rounds and converts internally. Here we are storing our values independent of the _alpha of the target, so what we set the alpha to in the modifier, is what we get when we retrieve the value from the modifier.

This class is really simple to use, relying on object composition, here is a simple example to show the benefits from before.

var modifier = new AlphaModifier(_root);
modifier._alpha = 0;

for (var i = 0; i<100; i++) {
    modifier._alpha ++;
    trace(modifier._alpha);
}



If you have set up your class right, and you run this code, you will see that there is no innacuracies, you're alpha will be printed from 0-100. This is a small workaround, but it works.

Take Care.

_Michael