Task: Orbiting Planets

I l @ ve RuBoard

In the trigonometry example earlier this hour , you learned how to make a movie clip go around in a circle. Let's carry that concept even further by making a movie showing four planets orbiting the sun. In addition, the third planet will have a moon orbiting it.

  1. Create a new movie.

    Make six movie clips from simple circles. Name them sun, mercury , venus, earth, mars, and moon. Give them the same instance names as their library names . You can make them different sizes, relative to each other. They should all be fairly small. See the example movie 11planets.fla to get the idea of their sizes.

  2. Position the sun movie clip in the center of the screen. All the planets will orbit around the center of this movie clip. The positions of the other movie clips don't matter because we will take control of their positions with ActionScript right away.

  3. Attach this script to the mercury movie clip:

     onClipEvent(load) {     speed = .4;     radius = 40;     orbit = 0; } onClipEvent(enterFrame) {     orbit += speed;     this._x = Math.cos(orbit) * radius + _root.sun._x;     this._y = Math.sin(orbit) * radius + _root.sun._y; } 

    The load event sets the speed of the planet and the distance it remains from the sun. The speed determines how much of the circumference of the circle the planet travels in each frame. A speed of 6.28 would mean that the planet would move completely around the sun in one frame. A speed of .4 would mean that it would take 15.7 frames (6.28/.4) to orbit the sun.

    Every enterFrame , the orbital position is increased by speed , and the new position is calculated. Both x and y positions are adjusted by the location of the sun so that the planet's orbit is centered on the sun.

  4. Test the movie. All the movie clips except mercury should remain in place; mercury should quickly orbit the sun. If the orbit looks lopsided, you may have to edit the movie clips to make sure that the planets and sun graphics are centered in their own movie clips.

  5. Attach the same script to the venus, earth, and mars movie clips. However, use different numbers for the speed and radius variables . Each planet should get farther from the sun and should have a slower speed so that it takes longer to complete an orbit. In the movie 11planets.fla, the speeds of mercury, venus, earth, and mars are .4, .2, .1, and .05. Likewise, the radius for the orbit of each planet is set to 40, 90, 150, and 210.

  6. Test the movie again. All four planets should now orbit. The farther the planet is from the sun, the longer it should take for it to complete an orbit. The moon hasn't been programmed yet, so it will remain in place.

  7. Use this script for the moon. It has a fast speed and a small orbital radius. It also orbits the earth rather than the sun, so the enterFrame handler modifies the new location using the location of the earth rather than the sun.

     onClipEvent(load) {     speed = .5;     radius = 15;     orbit = 0; } onClipEvent(enterFrame) {     orbit += speed;     this._x = Math.cos(orbit) * radius + _root.earth._x;     this._y = Math.sin(orbit) * radius + _root.earth._y; } 
  8. Test the movie again. It should all work now, with one exception. The moon orbits slightly off-center from the earth. Why? Well, the moon's location changes first because it is in front of the earth in the movie's layering. So the moon's position is set relative to the location of the earth, and then the earth moves slightly. We need to make the earth move first and then the moon. To do this, select the moon movie clip and choose Modify, Arrange, Send to Back.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net