Recipe 16.6. Optimizing Download Time by Sharing Content Among Movies


Problem

You are loading multiple .swf files into a single application using one of the techniques discussed in this chapter, and two or more of the .swf files happen to share one or more common elements, such as a movie clip, a sound, a bitmap, or a font. Rather than loading the same element multiple times, you want to load the element once and share it amongst the multiple .swf files.

Solution

Use a shared library.

Discussion

Shared libraries are a good way to reduce overall download size when the same element or elements are used across multiple .swf files in a single project. For example, if you have several .swf files that use the same embedded font, and if you embed the font in each of those files, you could be adding 10, 15, or even 20KB to the file size of each. Obviously that is not the greatest solution, because if you use that font in five .swf files in the same project, you could be adding 40 to 80KB to what the user has to download unnecessarily. Instead, what you can do is place the font in a shared library, link each of the .swf files to the shared library, and then require that the user only download the font once. The same idea applies not only to fonts, but to other types of elements including movie clips, buttons, graphics, bitmaps, and sounds.

There are essentially two steps when working with a shared library: creating the shared library and linking the .swf files to the shared library. Each of these steps is quite simple. To create the shared library:

  1. Open a new .fla document, and save it to the same directory as the other files for your project.

  2. Add the shared symbols to the library:

    • For movie clip, button, or graphic symbols, you may either create the symbol from scratch or copy the symbol from the library of another .fla file.

    • For sound and bitmap symbols, you may either import the symbol or copy the symbol from the library of another .fla file.

    • For font symbols, you may either create a new font symbol or copy a font symbol from the library of another .fla file. To create a new font symbol, choose the New Font option from the library's menu and follow the prompts in the dialog box.

  3. Open the linkage settings for a symbol in the shared library. You can open the linkage settings by right-clicking/Command-clicking the symbol in the library and choosing Linkage from the context menu.

  4. Check the Export for runtime sharing checkbox. The Export in first frame checkbox will automatically be selected. You can leave it checked.

  5. In the URL field, enter the name of the .swf file as you plan to export it. For example, if you intend to export the .swf for the shared library as sharedAssets.swf, enter that value in the URL field.

  6. Click the OK button to close the dialog box.

  7. Repeat steps 3 through 6 for each symbol in the shared library.

  8. Export the .swf file for the shared library document. Make sure to name the file with the same name you set in the URL field for each of the symbols' linkage settings.

To link the shared assets from the shared library to the libraries of the other documents that will use them:

  1. If the shared library Flash document is not already open, open it and make sure that the library is opened.

  2. Open a Flash document that will use the use the shared symbol(s), and make sure that its library is opened.

  3. You should be able to see the libraries for both documents at the same time. Drag all of the linked symbols from the shared library to the library of the Flash document that will use them. If you already have any symbols with the same names in the document, Flash will prompt you for what to do. In such a case, choose the "Replace existing component" option. When you drag a symbol from the shared library into another Flash document's library, Flash knows to link to the shared library asset rather than export the symbol in the .swf file.

  4. Use the linked symbols:

    • For movie clip, button, graphic, bitmap, and sound symbols, use the symbols as you would if they were normal symbols. For example, you can drag a linked movie clip symbol onto the stage. If you replaced any existing symbols when completing step 3, you should not need to make any additional changes with respect to how the existing instances of those symbols are used.

    • For font symbols, you should make sure that for any text fields in which you want to use the shared font, you have selected the shared font name from the Font menu in the Property inspector. The font will show up in the list with the font symbol name followed by an asterisk. Thus if you named the font symbol NeatFont Shared, then the option will show up in the Font menu as NeatFont Shared *. Static text embeds the font by default, but if you are using the shared font with a dynamic or input text field, make sure to embed the font following the instructions in Recipe 8.14. Although it is possible to use shared fonts with text fields created with ActionScript code, it is beyond the scope of this book to describe how to do that (see the ActionScript Cookbook, O'Reilly, for more information).

  5. Repeat steps 2 through 4 for each Flash document that will use the shared assets.

When you publish your project, you must make sure to include the shared library .swf with the other .swf files.

See Also

Recipe 8.14




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

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