4 Replies Latest reply on Jan 5, 2007 6:09 PM by kglad

    Flash _alpha losing accuracy

    Yerwol Level 1
      I have this piece of code:

      this._alpha = Math.round(this._alpha + this.fadespeed);
      trace("before:" + this._alpha);
      this._alpha = Math.round(this._alpha);
      trace("after: " + Math.round( this._alpha));

      which is used within a hit test to fade in/out a MC when the mouse hovers over it. For some reason though, even though _alpha starts off as 0, and this.fadespeed = 5, and i have the Math.round function there, i get:

      before:4.6875
      after: 5
      before:9.765625
      after: 10
      before:14.84375
      after: 15
      before:19.921875
      after: 20
      before:25

      Anyone know where im going blatantly wrong, or if i need to do an explicit data type cast anywhere. The Math.round function is returning the correct value, just the _alpha variable in the MC doesnt want to hold integers it seems!!!

      Im using Flash MX 2k4
        • 1. Re: Flash _alpha losing accuracy
          kglad Adobe Community Professional & MVP
          flash stores the _alpha property internally as an integer between 0 and 255.

          normally, the conversion doesn't cause a problem. but when you repeatedly increment or decrement the _alpha of a movieclip the errors can accumulate.

          to remedy use a variable to store the _alpha value of your movieclip. update that variable's value and assign your movieclip's _alpha to be the value of the variable.
          • 2. Re: Flash _alpha losing accuracy
            Yerwol Level 1
            Thanks. I tried doing that before as a way to fix it, just did a "this.alphavalue" and alter that instead. Seems to work okay!

            Thanks for the advice :-)
            • 3. Re: Flash _alpha losing accuracy
              Level 7
              > flash stores the _alpha property internally as an integer between 0 and
              > 255.

              Actually .. it stores that value as an integer between 0 and 256; where 0 =
              0% alpha and 256 = 100% alpha. That's why you can get accurate results with
              (say) 50% (which translates exactly to an internal value of 128.

              I know why you might thing it should be 255 .. but its not :)

              Other than that, your explanation was correct :)
              --
              Jeckyl



              • 4. Re: Flash _alpha losing accuracy
                kglad Adobe Community Professional & MVP
                that is strange. but yes, the _alpha stored by flash is the same as one would expect when the _alpha is 100*i/256; for some integer i = 0,...,256.