Sometimes the users don’t need the whole information available on your flash movie. When you create Flash applications it will take much more time to load if you have the whole content in one file than if you load the content user wants from external sources.

loadMovie() method is one of the methods of Flash to load dynamically external content.

The loadMovie() method
The loadMovie() is a method used for loading external content (.swf, .jpg, .gif and .png files are supported) directly into a MovieClip object.

The syntax is simple for loadMovie(), that could be used in two ways:

1. loadMovie(URL:String, movieClip: Object, [method:String]);
2. movieClip.loadMovie(URL:String, [method:String]);

Regardless of the way we use it, loadMovie() does exactly the same thing.
URL - represents the path (relative or absolute) to the external file to be loaded;
movieClip - is the movie clip where the external file will be loaded;
method - is an optional parameter that represents the HTTP method for sending and receiving variables. It can take only two values: GET (that will append the variables to the end of the URL) or POST (that will send the variables in a separate HTTP header).

Now let’s see how loadMovie() actually works in the next example where a random picture will be loaded and display at runtime.

STEPS TO FOLLOW:

1. Prepare 3 nice pictures in .jpg format having dimensions 300×200 and save them in the same folder. Give them the names picture1.jpg, picture2.jpg2 and picture3.jpg.

2. Create a new flash document and save it as loadMovie.fla in the same folder with the images from step above.

3. Go to Modify >> Document (Ctrl+J) and set the width to 300 pixels and the height to 240 pixels.

4. Insert a new layer and rename the top layer actions and the bottom one button.

5. In the first frame of the button layer put your favossrite button from Window >> Common Libraries >> Buttons and align it to the bottom edge of the Stage. Go to the Properties panel and give it an instance name of loadButton_btn.

6. Now, in the first frame of the actions layer open the Actions panel (F9) and put in the following ActionScript code:

// create a movie clip to hold the picture
this.createEmptyMovieClip("holder_mc", this.getNextHighestDepth());
//create the event for the loadButton_btn
loadButton_btn.onRelease = function(){
 // r is a random number between 1 and 3
 var r:Number=Math.floor(Math.random()*3)+1;
     //load the random image
     holder_mc.loadMovie("picture"+r+".jpg");
}

7. Great now! Test the movie and see how you load images dynamycally using ActionScript loadMovie() method when you click the button.

The unloadMovie() method

If you want to clean out the content of a movie clip you can use the unloadMovie() method.

This method could be used in two ways, too:

1. unloadMovie(movieClip);

2. movieClip.unloadMovie().

Hope this tutorial was helpful!

One Response to “Loading external content in Flash with loadMovie() method”
  1. suresh says:

    I have done loadmovie()

Leave a Reply