LOADING MOVIES INTO A LEVEL


Thus far, we've loaded external content into an existing movie clip instance. However, you can also load external movies and JPGs into levels. You can think of levels as layers of movies (SWFs) that exist within the Flash player window, all at the same time. For example, when you view an HTML page containing a Flash movie, a Flash player window is created and the initial movie (as identified in the <object> and <embed> tags) is loaded into the player. That movie is loaded into a z-plane (a term signifying depth) within the player window known as Level 0. You can load additional SWFs into higher levels in the player window.

graphics/17fig13.gif

You do this by employing a variant of the loadMovie() action you've used up to now:

 loadMovieNum("myExternalMovie.swf", 1); 

There are two differences between this code and that used to load an external asset into a target/instance: First, the action is now named loadMovieNum() rather than loadMovie() . In addition, instead of identifying a target for the external asset, we've identified a level number. This action loads myExternalMovie.swf into Level 1 of the Flash player window.

It's important to keep in mind the following when loading a movie into a level:

  • Only one SWF (or JPG) can occupy a level at any time.

  • You don't need to load movies (or JPGs) into sequential levels. (For example, you can load a movie into Level 50 even if Levels 1 through 49 are empty.)

  • The content of movies on higher levels appears above the content from levels below it. (In other words, the content of a movie on Level 10 will appear above all content on Levels 9 and lower.)

  • The frame rate of the movie loaded into Level 0 takes precedence, dictating the rate at which all movies in the Flash player window will play. (For example, if the movie on Level 0 were set to play at 24 frames per second and an external movie, with a frame rate of 12 fps, is loaded into a target or level, the externally loaded movie's frame rate will be sped up to match that of the movie on Level 0.) Be sure to set similar frame rates when authoring your various movies; otherwise, you may get unexpected results in the final product.

  • If you load a movie into Level 0, every level in the Flash Player will be automatically unloaded (removed), and Level 0 will be replaced with the new file.

In this exercise, we'll enable our project to randomly load banner movies into levels.

  1. Open virtualaquarium4.fla in the Lesson17/Assets folder.

    This is the file we worked on in the previous exercise. In this exercise, we'll add additional script to Frame 1. Because we'll be loading our banner movies into levels, as opposed to targets, you don't need to use any of the elements in the current scene to make the process work. A rectangular graphic (with an instance name of bannerback) has been added to the scene for design purposes.

    Before going any farther, let's once again review the contents of the directory that contains our externally loaded assets.

  2. Using your operating system's directory-exploring application, navigate to the Lesson17/Assets directory.

    Locate the following movies:

    • banner0.swf

    • banner1.swf

    • banner2.swf

    Our project will be set up to randomly load one of these simple animated movies (each of which is 200 pixels W by 60 pixels H) into a level.

  3. Return to Flash. With the Actions panel open, select Frame 1 of the Actions layer and add the following line of script just below where you created the slides array in the previous exercise:

     banners = new Array("banner0.swf", "banner1.swf", "banner2.swf"); 

    This creates a new array, named banners, which holds the paths to our external banner movies.

    Now let's create a function that will randomly load one of these banner movies into our project.

  4. Place the following function definition just below the changeSlide() function definition:

     function randomBanner() {    randomNumber = random (banners.length);    loadMovieNum (banners[randomNumber], 1);  } 

    graphics/17fig14.gif

    This creates a function named randomBanner() that works similarly to the randomBackground() function we set up in the first exercise in this lesson. Notice the slightly different name of the action that executes the loading function (loadMovieNum() ), as well as the fact that we've indicated that it should load the movie into Level 1. Each time this function is called, a random banner is loaded into Level 1, replacing the one already there.

  5. Place the following function call just below the changeSlide() function call:

     randomBanner(); 

    This function call is used to load the initial banner when the movie begins playing. Next, to make better use of the function, we'll use a setInterval() action to automatically call this function on a regular basis.

  6. Place the following line of script just below the randomBanner() function call:

     setInterval(randomBanner, 10000); 

    Using the setInterval() action, we've scripted the randomBanner() function to execute every 10,000 milliseconds (once every 10 seconds). This will provide our project with rotating banner functionality. You can use the setInterval() action to call any function or object method on a regular, timed basis.

  7. Choose Control > Test Movie.

    As soon as the movie begins to play, a random banner is loaded into the project. The main movie is loaded into Level 0 automatically, while the banner is set to load into Level 1 which means it's loaded above everything else in the project. If you replay the movie, chances are a different banner will load. As you will notice, however, there's a problem: The banner's position on the stage isn't correct; it should appear above the white rectangular box.

    graphics/17fig15.gif

    The reason for this is that the banner movie's top-left corner (its registration point) will, by default, load at a position of 0x and 0y in relation to the registration point of the movie on Level 0. In essence, the top-left corner of the loaded movie is automatically aligned to the top-left corner of the movie on Level 0. This is true of any movie loaded into any level.

    However, because our banner movie is only 200W by 60H, a spacing discrepancy exists when it's loaded into a 550W by 400H movie. When the banner movie is loaded, it needs to be repositioned directly above the white rectangle on the stage something we'll correct in the next exercise.

  8. Close the test movie, and save the file as virtualaquarium5.fla.

    This completes this exercise. We'll continue to build on this file in the following exercises.



Macromedia Flash MX ActionScripting Advanced. Training from the Source
Macromedia Flash MX ActionScripting: Advanced Training from the Source
ISBN: 0201770229
EAN: 2147483647
Year: 2002
Pages: 161

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