So it happens that the value of alpha that you assign to a given DisplayObject, say dispObj, is not equal to the value that is actually set. Try dispObj.alpha=0.8; and on the next line trace(dispObj.alpha). The traced value is about 0.7969. This discrepancy might be especially bothersome when you are programming fade in - fade out transitions. Why the discrepancy and how to minimize its effects?

In the applet above, we have a MovieClip, coloredRect, on the Stage. Its alpha is initially set to 0. The Alpha Plus button at each click ads 0.1 to the current value of coloredRect.alpha. After 10 clicks, you would expect coloredRect.alpha to be equal to 1. In fact it is 0.9766, so in the end our object is not completely opaque. This is a significant difference and it would be even larger if you had more steps. One way to minimize the effects of this discrepancy, is not to use the current value of alpha to set the next value as that causes error to accumulate.

In the applet below, instead of using the current value of coloredRect.alpha, we set the next value of coloredRect.alpha based on the value of a global variable whose value represents alpha that we want to be the next value for coloredRect.alpha. There is still a discrepancy, but much smaller as the error does not accumulate. Also, in the end our object is opaque.


  • Download all the source files for this How-To: alpha.zip

Why the Discrepancy?

The reason for the diffrence between the assigned and the actual values of alpha (as we understand it) is as folows. Each value of coloredRect.alpha is stored internally as one of the 256 values, namely as an integer between 0 and 255. When we assign coloredRect.alpha=0.1, we are saying that we want 10 percent of completely opaque. What integer among the 256 integers corresponds to 10 percent? 10 percent of 256 is 0.10*256=25.6 which is not an integer. Thus, it is being internally rounded down by taking Math.floor(0.10*256). Rounding down gives the value 25. 25/256=0.0977 thus 25 corresponds to 9.77 percent of 256. Therefore, after you assign coloredRect.alpha=0.1, the actual alpha becomes coloredRect.alpha=0.0977. (We rounded off all values to four decimal places for the sake of this presentation.)

In general, when you assign:


the actual value of alpha becomes:


See the next page for the short code for both examples on this page.

Back to AS3 How To and Tips              Back to Flash and Math Home

We welcome your comments, suggestions, and contributions. Click the Contact Us link below and email one of us.

Adobe®, Flash®, ActionScript®, Flex® are registered trademarks of Adobe Systems Incorporated.