4 Replies Latest reply on Jan 17, 2011 10:42 AM by vb_k

    Weird problem with +=

    vb_k

      I'm trying ot do a fade in using += 10 on alpha and a onEnterFrame function.

       

      It doesn't sound too troublesome, I myself did it already 2 zillion times.

       

      To test if I'm not doing anything wrong, I always do a first test deleting the onEnterFrame with a smaller _alpha, like 60. When i tried this turn, the onEnterFrame wasn't being deleted. Code:

       

      onEnterFrame = function (){
      _parent.content._alpha += 10 ;
      if (_parent.content._alpha = 60){
      delete this.onEnterFrame;
      }
      }

       

      I decided so to trace the _alpha property, and here is the result:

       


      0
      9.765625
      19.53125
      29.296875
      39.0625
      48.828125
      58.59375
      68.359375
      .....

       

      Anyone knows why flash might be implementing a number different than what I've set on code?

        • 1. Re: Weird problem with +=
          kglad Adobe Community Professional & MVP

          internally flash stores the _alpha property as a hexadeciaml: 0 to 0xFF (0 to 255).  so, it rounds decimals to a hexadecimal.

          • 2. Re: Weird problem with +=
            landar

            The reason the delete is not happening is because you have specified an exact alpha value to hit.(60)

            Use ">=" and try:

             

            if (_parent.content._alpha >=  60){
            delete this.onEnterFrame;
            }

            • 3. Re: Weird problem with +=
              landar Level 1

              Concerning the alpha value, there is a rounding/truncation issue going on in Flash so that every time you read the alpha value back, it is slightly different from what you set. You may notice, however, that values of 0, 25, 50, 75, 100 are exactly as set. But you are not hitting those values because the rounded alpha value will not allow it to hit them when 10 is added.

               

              You could use a static or global "temp_alpha" to increment and set your scene alpha. This would not suffer from the accumulation of rounding errors.

               

              var temp_alpha = 0;

               

              onEnterFrame = function (){
              _parent.content._alpha =  temp_alpha ;

              temp_alpha += 10;
              if (temp_alpha >= 60){
              delete this.onEnterFrame;
              }
              }

               

              You will have to be careful about the scope of temp_alpha so that it stays persistant.

              • 4. Re: Weird problem with +=
                vb_k Level 1

                Thank you!

                 

                I had solved the problem already when posted, using the >= instead just ==, but the main reason i posted was to find out why the increment was broken to not-precise decimals =).

                 

                So thanks for the replies now I know it better! ^^