Preloading Your Sound and Animation


Because sound files are by their nature large, whenever you're adding sound, particularly sound that has been synchronized to animation, it's a good idea to make sure that all the sounds have loaded before the movie begins to play. You can do that by adding in a preloader.

Preloaders can be simple or complex. You'll add in a very simple one here. You'll take a look at preloaders in more detail in Chapter 16.

Exercise 13.8 Adding a Preloader

Okay, this is an easy one. You deserve a break about now. So far, you've been working with just one frame in your main movie. Now you're going to add some extra frames and set up a check to see if the whole movie has loaded yet.

  1. You still should have NewYork.fla open, or you can open NewYork2.fla from the CD.

  2. Make sure you are working on the main timeline. Select frame 20 in all layers and press F5 to extend the timeline.

  3. Select frame 1 of all layers except the Actions/Labels layer, and drag those keyframes to frame 20.

  4. Add a new layer just below the Actions/Labels layer and name it Load.

  5. Select the Text tool. Open the Character panel (Window > Panels > Character) and change the following settings:

    Font: Arial or Helvetica

    Size: 16

    Color: White (#FFFFFF)

  6. With the Load layer selected, type the following in the lower-left corner of the Stage:

    Load

  7. Still in the Load layer, insert a keyframe (F6) in frame 5. With the Text tool, click the word Load and add a single period at the end of the word.

  8. Repeat Step 7 for frames 10 and 15, adding an additional period to the end of the Load phrase each time. Insert a blank keyframe (F7) in frame 20. (See Figure 13.13.)

    Figure 13.13. The Load layer has a four-keyframe loading sequence with a blank keyframe in frame 20.

    graphics/13fig13.gif

  9. Save your file.

When the movie starts, it'll play the new loading sequence you just created, but you don't want the rest of the movie to play until all the assets have loaded. You'll need to build what's called a "gate" to handle that.

Exercise 13.9 Building a Gate

On the first frame of the Actions/Labels layer, you'll test whether everything has loaded. If it has, you'll jump straight to the frame where the rest of the movie starts. If it hasn't, you'll play through the loading sequence. At the end of the loading sequence, you'll send the movie back to frame 1 again to test if everything has finished loading.

  1. You should still be in sound.fla. Select frame 20 of the Actions/Labels layer and open the Frames panel (Window > Panels > Frame). Assign frame 20 a label of Begin.

  2. Still in the Actions/Labels layer, select frame 1 and launch the Actions panel. Frame 1 currently has one action, the one where you set the value of the variable music to true. In Expert mode, add a new blank line after the line with the music variable. You're going to set a new variable called Percent here.

    You're going to do something a little different for the value of the variable this time. Movie clips are objects in Flash 5. You can apply certain methods to movie clip objects. Don't stress about thisyou're going to cover object-oriented programming in Chapter 14, "Introduction to Object-Oriented Programming." All you really need to know right now is that there are two methods that you can use to figure out whether a movie has completely loaded:

    • getBytesLoaded() checks how many bytes of information have already loaded.

    • getBytesTotal() checks how many bytes are in the entire movie, including any movie clips in that movie.

  3. In the Actions list, type the following:

     percent = getBytesLoaded() / getBytesTotal(); 

    Spelling and capitalization count, so check them twice.

    As long as the number of bytes loaded is less than the total number of bytes, the value of percent is less than 1. As soon as all the bytes are loaded, percent is equal to 1. You can check that with a simple if statement. If percent is equal to 1, it's okay to go ahead and jump to the frame you labeled Begin.

  4. Add a new line after the line you just entered, and type the following:

     if (percent == 1) {     gotoAndStop("Begin");  } 

    Your completed code should look like the following:

     music=true;  percent = getBytesLoaded() / getBytesTotal();  if (percent == 1) {     gotoAndStop("Begin");  } 
  5. You have one only more thing to do to finish your preloader. In the Actions/Labels layer, insert a keyframe in frame 19. Open the Actions panel and type the following:

     gotoAndPlay(1); 
  6. Save and test your movie.

That's all you have to do. Now when the movie starts, it checks whether percent is equal to 1. If it's not, the movie continues to play through to frame 19, which sends it back to the beginning. If percent is equal to 1, the movie jumps past frame 19 to frame 20, which is labeled Begin.

Now you have a movie with synchronized sound and animationall of which is preloaded. You can turn the sound off and on. Life is good.



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

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