Answered
Unexpected FOR loop result
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.
<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.