Flash 8 Cookbook | Page 3 | WebReference

Flash 8 Cookbook | Page 3


[previous]

Building Preloaders

20.4 Using a Progress Bar to Create a Graphical Preloader

Problem

You want to create a preloader that displays a progress bar indicating how much of the movie has loaded.

Solution

Modify the script from Recipe 20.2 to update the _xscale property of a movie clip.

Discussion

Although simply displaying a text-based progress indicator to the user (as in Recipe 20.3) may work for some applications, you'll more frequently want to display the progress in some sort of visual, animated manner. The most common such indicator is the progress bar. The progress bar, as the name suggests, is a rectangular shape (the bar) that indicates the progress as the file downloads. Although you can certainly make more elaborate sorts of indicators, the progress bar is the standard, and the basic principals in creating a basic progress bar apply to any progress indicator.

The script in Recipe 20.2 takes care of the majority of the work in creating any sort of basic preloader—graphical or not. So you'll want to use the same script when adding a progress bar. However, you'll want to then add just one line of code (shown in boldface):

The new line of code tells Flash to adjust the scale in the x-direction for a movie clip called mProgressBar such that it corresponds to the percentage of the file that has downloaded. Therefore, in order for the code to be effective, you must add a movie clip instance to the stage with an instance name of mProgressBar. That movie clip ought to contain rectangular artwork that is left-aligned (meaning that the left edge of the rectangle shape is positioned at 0).

20.5 Creating Preloaders for Files with Exported Symbols

Problem

Your have a file that exports movie clips for ActionScript, and you want to add a preloader.

Solution

Use the same preloader principles discussed earlier in this chapter. In addition, uncheck the "Export in first frame" option for each exported movie clip within the library.

Discussion

As you learned in Recipe 11.17, you can tell Flash to export a movie clip symbol even if it is not used at authoring time. That enables you to programmatically add instances of the movie clip using ActionScript. With a few simple movie clips exported for ActionScript, you won't likely notice any effect on the preloader. However, with many complex (particularly bitmap) movie clips that are exported for ActionScript, you may start to notice that the preloader doesn't appear immediately. It's possible that the preloader may not even appear until most of the file has downloaded. This is due to the default setting for movie clips exported for ActionScript— they are set to export on the first frame. Thus every exported movie clip must download before Flash will play back even the first frame.

There is a relatively simple solution, however. Although the default setting is such that Flash-exported movie clips are exported on the first frame, you can tell Flash to use an alternate frame instead, with the following steps:

  1. Uncheck the "Export in first frame" option for each movie clip symbol. You can access the option from the Linkage properties dialog box. By default, the "Export in first frame" option is checked when you check the "Export for ActionScript" option. However, when the "Export for ActionScript" option is selected, you can independently toggle the state of the "Export in first frame" option.
  2. Place instances of the movie clips on the stage at authoring time. That way Flash knows what frame to use in order to export the symbol. The second step may seem counterintuitive. However, it is simply the way in which you communicate to Flash where to export the symbol. The dilemma that arises is then that you frequently don't want those symbols to appear in the exported movie on any frame unless they were placed there with code. So placing an instance on the stage at authoring time seemingly defeats the purpose, or at least causes an obstacle. The solution is to approach the Flash project in a somewhat nonlinear fashion. Even though a timeline plays from first frame to last frame by default, there's no reason it has to do that. As you've already learned, you can use simple ActionScript commands to tell Flash to stop, play, and jump to any frame within a timeline. Therefore, you can always place the movie clip instances on a frame that is never played. For example, you can place the preloader on frame 1, the movie clip symbol instances on frame 2, and then the actual content starting on frame 3. Within the script on frame 1, you can instruct Flash to jump to frame 3 once the file has downloaded.

20.6 Creating Preloaders for Files with Components

Problem

Your movie contains components, and they prevent the preloader from appearing until the movie is almost entirely loaded.

Solution

Use the same principles as discussed in Recipe 20.5. Additionally, assign a frame other than 1 to the Export frame for classes field.

Discussion

Components are special movie clips. And by default they are set to export on the first frame. That means that in order to properly preload an SWF that uses components you must employ the same strategy as is discussed in Recipe 20.5. However, in addition to the challenges presented by exported movie clips, components present a new challenge. Unlike standard exported movie clips, components are composed not only of graphical elements, but they consist also of ActionScript code. That ActionScript code is placed in special structures called classes, and those classes can account for rather significant file size. For example, the Loader component is approximately 27KB, almost all of which is due to the code in classes.

Like exported movie clips, classes export on the first frame by default. That means that all the code in all the classes used by an SWF must download before the first frame can play back, during which time the the user waits at a blank screen for seconds or even minutes. However, it's remarkably simple to adjust the export frame for the classes used by a Flash file:

  1. Open the Publish Settings by selecting File . Publish Settings.
  2. Select the Flash tab.
  3. Click on the Settings button to the immediate right of the ActionScript version menu.
  4. Enter a frame number greater than 1 in the Export frame for classes field.
  5. Click OK.

In order for a component to properly work, you must make sure that the classes it requires are exported on a frame before the component is used.


[previous]

URL: