- Designing Your Web Site to Be Modular
- Loading Movies or JPGs
- Task: Use Load Movie
- Determining When a Movie Is Fully Loaded and How to Unload It
- Task: Determine Whether a Movie Is Fully Loaded
- Shared Library Items
- Task: Prepare Items to Share at Runtime
- Task: Start Using a Shared Item
- Task: Update Shared Items in a Library
- Task: Share a Font During Runtime
- Linked Scripts
Task: Use Load Movie
In this task you use the Load Movie Action to selectively let the user download just the segments he or she is interested in. Here are the steps to follow:
The name of the game for this task is organization. Remember, haste makes waste. Start with a new empty folder into which you'll save and export all your movies.
Create a new file and set the movie's width and height both to 300. Use Modify, Document (or Ctrl+J) to do this and make sure that Ruler Units is set to Pixels.
Create a simple tween of your choice, but make the tweening object entirely red. Save the movie as red.fla in your folder created in step 1. Do a Test Movie, which will export a movie called red.swf in the same folder as your red.fla file.
Do a Save As, name the file green.fla, and change the color of the tweening object to green. (You may need to change the color in each keyframe.) Remember to save and then Test Movie to create the .swf.
Repeat step 4 but create a file with everything blue.
You should have three .flas and three .swfs (red, green, and blue for both). Close all the Flash files. Then create a new Flash file and save it as main.fla in the same folder. Set this movie's size to 500x500.
This "main" file will load movies into a clip. Draw a square exactly 300x300 (draw any rectangle and then use the Info panel to change its dimensions to 300x300). Make sure there's a line around the box and then delete the fill. Select the entire box and convert to it a symbol (make it a Movie Clip and name it "box"). Name the instance on the Stage "theClip" in the Properties panel while the box is selected.
Now draw a rectangle in the main scene (to become a button). Convert it to a symbol (button) and then access the Actions panel for this button instance. Insert a loadMovie Action (from the Actions section of the Toolbox list). Notice that, by default, your script will read loadMovieNum. Simply change the location drop-down (in the parameters area of the Actions panel) to Target (which really should be thought of as "clip"). For the URL parameter, just type red.swf, and then in the field to the right of Target, type theClip, because this is the name of the clip into which you want to load the movie (see Figure 19.1).
Figure 19.1 Load Movie requires that you specify what movie (URL) you want to load and whether you want to load it into a target (a clip) or a level number. Here, a clip instance name already on the Stage is specified.
Test the movie. It should work, except the movie loads into its box too far down and to the right. Although the movie that gets loaded is the same size as the box, it gets positioned with its top-left corner registered to the center of the clip into which it is loading.
The way to fix this is to go inside the master version of the "box" clip and move the contents (the box you drew) so that the top-left corner lines up with the "center" plus sign inside the Movie Clip (see Figure 19.2). You can use the Info panel while inside the Movie Clip to set the top-left corner of the selected shape to position 0,0. When you return to the scene, your clip appears to have moved, but really you've just repositioned where the contents load. Move the box to a location toward the center of the screen (basically, wherever you want it).
You can now make two more buttons and use the same basic Load Movie Action, except change the URL to point to green.swf and blue.swf.
Figure 19.2 Editing the relative center point of the master version of the clip in which you're loading movies will affect the positioning of the loaded movie. That is, the loaded movie's top-left corner corresponds to the clip's center point.
Flash MX added a simple but powerful enhancement to the loadMovie Action. As an alternative to specifying a .swf to load, you can now specify the filename of an external .jpg. This means you could have a tiny Flash movie that the user visits, but have countless native .jpg images that are loaded as the user requests. You could have an entire portfolio of photographs accessible through Flash. It's as simple as just specifying a .jpg file instead of a .swf in loadMovie's URL parameter.
Targeting Levels or Clips
In addition to learning how to use loadMovie, one lesson you learned in this task was that the top-left corner of loaded movies lines up with the center of the clip into which they are loaded. In the preceding task, you loaded movies into a clip. However, you can also load them into levels. Simply change the Target parameter to Level and type a level number into the field on the right (see Figure 19.3). Personally, I don't prefer loading into levels for two reasons. First, the positioning is always based on putting the top-left corner of the loaded movie in the top-left corner of the main movie, which makes registration difficult. Second, you have to keep track of level numbers (as opposed to clip instance names). It's not as though there's never a need to load movies into level numbers, but for the registration issues mentioned, I don't think doing so is as easy.
Figure 19.3 loadMovie can also load a movie (.swf) or image (.jpg) into a level number. You just have to specify "Level" from the Location parameter, and you must specify a number in the field on the right.
One of the cool things you can do with movies after they're loaded is address them the same way you can address clip instances. More specifically, you can set any property of a loaded movie just like you set any property of a clip. For example, you could stop the loaded movie by adding a button. In this new button, you would insert an evaluate Action and type theClip.stop(). (If you loaded the movie into level 1, you would just type _level1.stop().) Remember, there are a bunch of generic properties (such as _x, _y, _alpha, and so on), plus any homemade properties (that is, any variables you happen to be using inside the clip). For a practical example, consider the M3 snowboard site (it's located at http://www.m-three.com and pictured in Hour 3). Here, we loaded movies of various tricks. From the main movie, users can start, stop, and even jump to any frame in the loaded movie as they scrub.