6 Replies Latest reply on May 15, 2008 6:45 PM by kglad

    Unexpected FOR loop result

    cfords Level 1
      I am trying to increase the alpha property of a text field for a fade-in effect using a for loop when I discovered a problem with the loop itself. I added a trace statement to the following bit of code:

      <CODE>

      //For loop to increase alpha property of textframe by 1 until it reaches a value of 100 from 0. I have set
      //textframe._alpha = 0 in this.createTextField() ahead of this code.

      for (i=0; i<=100; i++) {

      textframe._alpha = textframe._alpha + i;
      trace ("Although (i) = " + (i) + ", textframe._alpha = " + textframe._alpha + " you NOOB.");

      }

      <END CODE>

      This loop yields the following:

      Although (i) = 0, textframe._alpha = 0 you NOOB.
      Although (i) = 1, textframe._alpha = 0.78125 you NOOB.
      Although (i) = 2, textframe._alpha = 2.734375 you NOOB.
      Although (i) = 3, textframe._alpha = 5.46875 you NOOB.
      Although (i) = 4, textframe._alpha = 9.375 you NOOB.
      Although (i) = 5, textframe._alpha = 14.0625 you NOOB.
      ...
      Although (i) = 100, textframe._alpha = 5031.25 you NOOB.

      I removed traces 6-99 for the purposes of saving space here. As you can plainly see, textframe._alpha does not equal (i) . This code always yields the exact same result to the decimal.

      (I know that I need to add a setTimeout() or setInterval() to delay the fade effect and I need to pause the text on screen and reverse the alpha effect for fade-out, etc.)

      I am "NOOB factor 10", this is true. However, I am making progress! Thanks ahead of time for any help pointing out to my pea brain why this is happening. I assume I have a timing issue not data type problem but I would not stake my life on the fact that I am correct :) All of this code is in a nested movieclip at frame 1. I am planning on developing this fade in/out effect over the same timeline whilst changing the text itself. I decided to go this route instead of trying to do this with clips on the stage, I thought that this way would give me more control and less drawing. See you in 8 days.
        • 1. Re: Unexpected FOR loop result
          kglad Adobe Community Professional & MVP
          flash stores the _alpha as a number x/256 where x/256 is the best approx to the alpha you assign.
          • 2. Re: Unexpected FOR loop result
            cfords Level 1
            Gee, good thing I DIDN'T stake my life on it. This revision ALMOST works, I am missing something in trying to convert the increment to an integer. It results in 100 at iteration 100 but is not quite right in the interim. It probably doesn't matter due to the way flash stores _alpha as you said, but it would be interesting to get this exact for noob's sake. I am reading about math.round as we speak. Thanks for pointing out x/256.

            for (i=0; i<=100; i++) {

            textframe._alpha = int(textframe._alpha/256 + i);
            trace ("Although (i) = " + (i) + ", textframe._alpha = " + textframe._alpha + " you NOOB.");

            }
            • 3. Re: Unexpected FOR loop result
              kglad Adobe Community Professional & MVP
              try:

              for (i=0; i<=100; i++) {
              textframe._alpha = i;
              integerPart = Math.floor(textframe._alpha);
              x = 256*(textframe._alpha-integerPart);
              trace(textframe._alpha+" = integerPart+x/256 = "+integerPart+"+"+x/256);
              }
              • 4. Re: Unexpected FOR loop result
                kglad Adobe Community Professional & MVP
                try:

                • 5. Re: Unexpected FOR loop result
                  cfords Level 1
                  Thank you! I should be able to incorporate the brunt of your example with the rest of my code and actually end up making this fade effect thing work. Not to mention walking away with a clearer understanding of what to study. As with most things the more you know the more there is to know etc etc. I got in way over my head with a previous forum topic that I have all but abandonded and ended up heading down this road. I don't like to leave topics hanging, should I follow up that topic with a giveup? Not sure what the protocol is here. At any rate, thanks again!
                  • 6. Re: Unexpected FOR loop result
                    kglad Adobe Community Professional & MVP
                    you're welcome.

                    and yes, it's not the worst thing in the world to recognize you're trying to do something that's too advanced for your current knowledge. if you stick with actionscript long enough, you'll get another chance later when your brain and experience may be up to the task without the suffering and frustrtation inherent in trying something you're unable to do.