Recipe 15.3 Loading an External JPEG Image

15.3.1 Problem

You want to load a standard JPEG graphic into your Flash movie from a remote URL.

15.3.2 Solution

Use the MovieClip.loadMovie( ) method.

15.3.3 Discussion

The loadMovie( ) method allows you to load not only .swf files but also JPEG images (.jpg or .jpeg files) into your Flash movie. The syntax and usage of the method is similar whether you are loading a .swf file or a JPEG:

// Load a JPEG into myMovieClip.

You should load external JPEGs into a holder movie clip nested within another movie clip because when you load any content into a movie clip using loadMovie( ), the movie clip's timeline is replaced by that content. When you load a JPEG into a movie clip, therefore, the movie clip's timeline is replaced by the image, and you can no longer control that object with the properties and methods of a movie clip. However, if the movie clip into which you load the JPEG is nested within a parent movie clip, then the parent can still be controlled as a movie clip (and the nested image is controlled correspondingly).

Here is a good methodology to follow when loading JPEGs into Flash movies:

  1. Begin with an existing movie clip or create a new movie clip using createEmptyMovieClip( ). This clip will act as the parent clip.

  2. Create a nested movie clip within the parent using createEmptyMovieClip( ). This is the movie clip into which the JPEG should be loaded.

  3. Call the loadMovie( ) method from the nested movie clip (not the parent clip).

  4. Once the JPEG has loaded, control the image using the parent movie clip.

For example:

// Create a parent movie clip.
_root.createEmptyMovieClip("myMovieClip", 1);

// Create a nested movie clip.
myMovieClip.createEmptyMovieClip("imageHolder", 1);

// Load the JPEG into the nested movie clip.

JPEGs, like .swf files, are loaded into Flash movies asynchronously (see Recipe 15.1 for an explanation). Therefore, the same need to monitor load progress applies. Furthermore, loadMovie( ) loads only nonprogressive JPEG files; other formats are not directly supported.

15.3.4 See Also

The global loadMovie( ) and loadMovieNum( ) functions can also load JPEG files. See Recipe 15.4 for information on loading files in other image formats. See Recipe 15.1 for more information on how to use the loadMovie( ) method. See Recipe 15.7 and Recipe 15.8 for more information on monitoring load progress.

    Part I: Local Recipes
    Part II: Remote Recipes