Graphics Programs Reference
In-Depth Information
Each time we enter the frame, we need to update the frequency angle (in degrees)
in line 35 so that the pendulum can rotate to a new angle. Line 36 uses something in
math and Flash known as the modulo function. The statement f = f % 360 means that
we are restricting the values of f to less than 360. It is the remainder that you would
get when you divide f by 360.
For example, 360/360 has a remainder of 0, 380/360 has a remainder of 20 and so
on. If we traced out the values, they would be 0, 20, 40, ... , 320, 340, 0. Why bother?
Well, we know the sin function repeats after 360 degrees, so there is no reason to go
higher than that, and it keeps the size of f from becoming unnecessarily large. The
greater good is in knowing how to restrict numbers to a range of values easily.
35
36
37
f += 20; // update the time factor
f = f % 360; // keep the range between 0 and 360
The calculation of the pendulum angle is straightforward and identical to what we have
done previously. What we next need to do is to stop the pendulum after 30 seconds.
We use an if statement to check to see whether the difference between the current
time and the starting time when we first clicked on the pendulum is equal to 30 seconds
(line 43). We need to use the Math.abs() function so that we always have a positive
number. Once time is up, we will stop the movement by deleting the onEnterFrame
handler and resetting the pendulum rotation angle to 0 (lines 45 and 46). Save your
movie as 5_4_clockWatcher.fla and test it.
38
39
40
41
42
43
44
45
46
47
48
49
50
// calculate the pendulum swing angle
angle = A * Math.sin( f * Math.PI/180 );
pendulum_mc._rotation = angle;
// if time is up, stop the pendulum & reset it
if ( Math.abs(mySecond - startTime) == 30)
{
delete pendulum_mc.onEnterFrame;
pendulum_mc._rotation = 0;
}
}
}
Search WWH ::




Custom Search