Graphics Programs Reference
In-Depth Information
Step 5: Add the circular motion
Adding the circular motion to each of the objects is pretty much a combination of using
the loop in Step 4 with the circular motion used in Exercise 5.1. As before, we will use
an onEnterFrame handler so that we can keep looping on the frame. We then need to
do some calculations for each of the rotating objects.
First we need to figure out at what angle to place each object on the circle. Referring
to Figure 5.22, we can see that object 0 starts at 0 degrees, object 1 at 45 degrees,
object 2 at 90 degrees, etc. So, in general, object i will start at i * 45 degrees or,
more generally, i * circleAngl e. We just need to add startAngle to correctly specify
the angle for each object.
19
20
21
22
23
24
25
26
27
28
// create the circular motion
this.onEnterFrame = function()
{
// for each rotating object ...
for (i = 0; i < numberOfObjects; i++)
{
// calculate the angle of the object
myAngle = i * circleAngle + startAngle; //in degrees
a = myAngle * Math.PI/180; //in radians
Once the angle has been determined, the position on the circle can be obtained by
applying the now familiar equations. Note the syntax this["object"+i] used. Flash
requires this notation when objects have been named in this manner through duplicat-
ing a movie clip. Updating startAngle by a negative number makes the objects and
the spokes spin in a counterclockwise direction. The amount specified is strictly your
choice, depending upon the speed you want.
29
30
31
32
33
34
35
36
37
// calculate the x- and y-coordinates
this["object"+i]._x = xc + r * Math.cos(a);
this["object"+i]._y = yc + r * Math.sin(a);
}
// update the startAngle increment and rotate the spokes
startAngle -= 2;
spokes_mc._rotation = startAngle;
}
Search WWH ::




Custom Search