Flash: Inaccuracies in the _alpha property

April 28th, 2005 by Luis

Have you ever realized about the rounding error when using the MovieClip._alpha property?

try this:

myMc.alpha=10; trace(myMc.alpha); // Displays 9.765625

This error could be very bad when you use fade transitions, making flash to run sluggishly. In order to avoid performance degradation you could use a Class or prototype that prevents this kind of error simply defining functions that read and write its own property.

Actionscript:
  1. class AlphaClip
  2. {
  3.     private var alphaInternal:Number;
  4.     private var target:Object;
  5.    
  6.     function AlphaClip (mc:Object)
  7.     {
  8.         target = mc;
  9.         alphaInternal = mc._alpha;
  10.     }
  11.     public function get _alpha ():Number
  12.     {
  13.         return alphaInternal;
  14.     }
  15.     public function set _alpha (alphaIn:Number):Void
  16.     {
  17.         target._alpha = alphaIn;
  18.         alphaInternal = alphaIn;
  19.     }
  20. }
  21.  
  22. //usage
  23.  
  24. var myAlpha:AlphaClip = new AlphaClip (myMc);
  25. myMc._alpha=10;
  26. trace(myMc._alpha); // Displays 9.765625
  27. myAlpha._alpha=20;
  28. trace(myAlpha._alpha); // Displays 10


Hack number 19 from Flash Hacks

Leave a Reply