Flash MX - Load and Unload Movies

Adobe Flash CS3 Professional
Flash CS3 introduces many new and exciting features and delivers an entirely new experience in Flash dev.
Adobe Flash CS3
Adobe Creative Suite 3
Streamline your web dev with a collection of Adobe software including the new Flash CS3 Pro.
Adobe Creative Suite 3
A nice way to keep Flash movie sizes down is to break them up into smaller pieces (movies) and only have those certain elements load when needed and have them unloaded when they're not needed.

Enter the Flash functions loadMovie(Num) and unloadMovie(Num). There are varying differences between loadMovie and loadMovieNum, and their unload equivalents. I'm not going to get into all that, so for this tutorial we are just going to use loadMovieNum and unloadMovieNum. They accomplish what we want to do.

The first thing you want to do is decide on your initial layout, then create it in what will be your base movie, or Level 0. Think of Levels as a tall building. The ground/bottom floor would be Level 0 (your base movie), then the next floor would be Level 1, and the next floor Level 2 and so on. The base movie that all of your other movies will load into will always be Level 0. So to load another movie, you'd need to place it on the next Level, which would be Level 1. Ok, so let's start and I can explain more along the way.

Step 1 - Start by creating a new Flash movie, 450 x 300. This will be the base movie, and will hold the externally loaded SWFs. Go ahead and create 2 buttons all the way to the left of the stage, and put one below the other. To keep track of everything, give them Instance Names of Button 1 and Button 2. You can make them any shape you want, I'm just using 2 rectangle shaped buttons. Let's go ahead and add the Actions to these buttons while we're here.

Step 2 - Bring up your Actions window for Button 1. Since the functions we're using are only available in Expert Mode, I can't step by step it in Normal Mode. There's not much code, so don't worry. Add this code to Button 1:

on (release) {
     loadMovieNum("button1_movie.swf", 1);
}


Do the same for Button 2, except you need to make the URL: button2_movie.swf. NOTE: If you make every movie load into the same Level (in this case 1), any newly loaded movie will replace the movie that was already on that level, if there is one. This would be preferable if you had multiple menu buttons opening their own movies, and you wanted a previously opened menu item to be closed before loading another menu item.

If you want to have the Button 1 AND Button 2 movies loaded at the same time, just change the Button 2 Level to 2. And if you had another, make it Level 3, and another Level 4, etc. For now, just leave both buttons set to Level 1.

Ok. We're pretty much done with this movie. If your movie looks like mine, you have 2 buttons at the left of the stage and alot of open stage area to the right of that. That open area is where your external SWF's content will appear. Make each external SWF the same size as your base movie. And you also want to keep the content to the right enough where they won't interfere with seeing the buttons.

(Non-working) EXAMPLE:   |   Working Example



If you'd like to be able to close the loaded movie itself without another menu button having to be clicked, then add a button to each external SWF and put this code in the button, replacing x with the number of the Level that movie loads into, which is 1 for this example:

on (release) {
     unloadMovieNum(x);
}


That should pretty much get you started in loading and unloading external movies.