|
||||||
|
||||||
Tutorial - Preloading External Flash Movies

Free
Flash Tutorial
![]()
This preloader tutorial shows you how to load an external Flash file into another Flash file with a preloader. This gives you the opportunity to break your Flash Movie into several more manageable parts rather than having one very large Flash file. It also means that the user need only wait for that section of the Movie that they wish to view to preload rather than waiting for a much larger file to load.
Cross Ref: There are many other Flash Pre-Loader tutorials including several which cover loading external Flash Movies and loading multiple external Flash Movies. To ensure that you are following the right tutorial for your needs, you may wish to read a brief description of each: Preloader Tutorial Listings
Example: Download
the Flash file Int 150a
Note: If you can see the Flash Movie (an animation) and not the preloader press Refresh (F5 or try Ctrl + F5) in your Browser. Once a Flash Movie has loaded the Browser may not truly refresh or re-load the Movie. Therefore if you did not see the preloader in action: Click here
Step One: Creating an External SWF Preloader
Tip: The Movie needs to be large enough to hold the external loaded swf Movies that you want to load. It is simplest if all the Movies are the same size.

Step Two: Setting up the Timeline



Step Three: The Preloader ActionScript
Step Four: Creating a Progress Bar
The progress bar is a visual representation of the percentage of the external Movie that is loading. It looks something like this:
Progress Bar.




Note: The Instance Name and Movie Clip Names are different. In the ActionScript it is always the Instance Name that is important. Remember that Instance Names are case sensitive and must not start with a number or contain spaces. The Movie Clips name can be anything you want (as long as each Movie Clip has it's own unique name). Instance Names do not need to be unique.
Cross Ref: For detailed information about how to name objects see the tutorial on: ActionScript Syntax
Progress Bar ActionScript
You now have to add some code to the Loading bar Movie Clip. What this does is control the width of the bar. So that as more of the object loads the wider bar becomes. The bars width will match the percentage. If 50% has loaded then the bar will be 50 pixels wide etc.
// When the Flash Movie loads do the following...
onClipEvent (load) {
// Call the following function
function follow(source, target, percent) {
/* Set the variable vector equal to the percentage of the content being loaded and minus current width of this Movie Clip. */
vector = (source-target);
// Set the desp variable equal to the vector variable times by the percent which is the percentage loaded
desp = (vector*percent);
// Return the amount loaded
return (source-desp);
// Reset the target value
dd.target = 0;
// Close the function
}
// Close the above Clip Event
}
// When this Movie Clip enters the frame (every 1/12th of a sec) do...
onClipEvent (enterFrame) {
// Call the function from above to reset this Movie Clip's width
this._width = follow(this._width, target, .2);
}
Note: The above code is attached directly to the outside of the Movie Clip not to the Timeline. In your Actions Panel it should say Actions - Movie Clip in the top left corner or the Actions Panel:
The Actions are attached to the Movie Clip not the Timeline.
Cross Reference: The comments in the ActionScript above are very brief and if you don't understand how the code works I suggest you look at one of the preloader tutorials where you will find a more in-depth explanation as to how preloaders work: Preloaders or Preloader Listings
Step Five: Creating the Preloader Movie Clip
The Progress Bar that you have just created is just part of the final display. We need to combine it with other information such as the percentage. This is done inside a new Movie Clip:


// Stop the preloader from automatically running
stop();

// Get the total bytes to be loaded
total_bytes = (this._parent.MyExternallyLoadedSWFMovieHolder.getBytesTotal());
// Get the bytes loaded so far
loaded_bytes = (this._parent.MyExternallyLoadedSWFMovieHolder.getBytesLoaded());
// Work out the difference between the total bytes to load and the bytes loaded so far
remaining_bytes = (total_bytes-loaded_bytes);
// Work out the percentage loaded
percent_done = (int((loaded_bytes/total_bytes)*100));
// Set the target bar percentage loaded
bar.ba.target = (percent_done);
// Display progress of percentage loaded in the text box
DisplayProgress = (Math.round(bar.ba._width))+" % loaded.";
// Check to see if everything has loaded
if (bar.ba._width>99) {
// If everything has loaded move onto the next frame
gotoAndPlay(4);
// If everything hasn't loaded then run this code
} else {
/* If everything has not loaded return to frame 2 and try again. This creates a loop and then runs the code above again. This is how everything updates. The values will change as more content is loaded. */
gotoAndPlay(2);
// Close the if-else statement
}

// Tell the external Movie to goto and play frame labeled "Play"
// You can use frame numbers too
_root.MyExternallyLoadedSWFMovieHolder.gotoAndStop("Play");
// Stop on this frame
stop();

The ActionScript Layer should now have three
little a's. The ActionScript
Layer is now finished.
The Text layer





Step Six: Creating the Timer Movie Clip
This Movie Clip has a small amount of ActionScript that controls the loading of the first external MC. Subsequently MCs are loaded when the user clicks on one of the buttons.

// This is how we automatically load the swf when the Movie loads
loadMovie(_root.MyVariable, _root.MyExternallyLoadedSWFMovieHolder);
// Tell the preloader to preload the first MC
_root.preloaderloader.gotoAndPlay(2);
// Stop this Movie Clip now the first MC has loaded
stop();
Step Seven: Placing the Timer on the Main Stage

Step Eight: The ActionScript
It is the ActionScript on frame 3 of the Main Stage that controls the bulk of the information that needed to preload the external swf Movie.
// Create a Movie Clip to load the swf Movie into
this.createEmptyMovieClip("MyExternallyLoadedSWFMovieHolder", 0);
/* Create a variable to hold the location of where the external swf Movie can be located and the file name of the swf Movie to be loaded in externally as an externally loaded swf Movie. e.g. the path to the image could be like c:\myFolder\MySWFMovie.swf or http://www.MyWebSite.com/myFolder/MySWFMovie.swf or if the swf file is in the same location you can use just the swf file name. */
// Change the URL below:
var MyVariable = "myMovie.swf";
// Set the loaded content's location on the Stage
MyExternallyLoadedSWFMovieHolder._x = 0;
MyExternallyLoadedSWFMovieHolder._y = 0;
// Stop on this frame
stop();
Important: Make sure you change the name of the file to the name of your file: myMovie.swf
Strange but True: It is the location of the web page and the Movie that is loading that is important not the location of this preloader Movie! If you wish to move this preloader Movie to a different folder you can. It can also be used several times on several different web pages as long as each time the web page is located in the same place as a Movie called: myMovie.swf. Each version of the Movie myMovie.swf could be a different Movie with different content!
Usually the Main Content Movie would be in the same folder as the web page. If it is not you will need to type a target path like this:
var MyVariable = "images/myMovie.swf";
You may also use a full URL like this:
var MyVariable = "http://www.webwasp.co.uk/images/myMovie.swf";Confused? If you get in a muddle the easiest thing is to have the Web Page, Preloader Movie and Main Movie all in the same folder.
The preloader Movie is now finished!!
Step Nine: The External
SWF Movie
In your external Flash Movie that you want to load you MUST add 2 blank frames at the very beginning on the Movie. It is probably easiest to just create a new scene. This first Scene must be the first thing to load. In the first frame you mush have a stop(); command and in the second frame you must add the label: Play and a play(); command. This is how to do it:




Note: The Flash Movie plays from the top Scene down which means that the Preloader will play after the Main Content. Not much point in that!!

stop();

play();
The External Flash Movie is now complete. Except of course you have to add the real content in the Main Scene.
Warning: If you create a Flash Movie that loops (such as the animation in the example at the beginning of this tutorial) then you need to make sure that the Main content layer(s) does not loop back to this first preloader Scene. Otherwise the stop(); will stop the loop and the animation will cease to animate. In my example that I used at the top of this tutorial I placed the following ActionScript at the end of the Main Content Scene:
gotoAndPlay (1);This causes the animation to loop back to frame1 of the Main Scene and play again. If I did not do this the Movie would (by default) go back to Frame 1 of the first Scene and stop. As the main Movie has already loaded the external preloader would no longer nude the main Movie onto action - so the stop would be permanent.
Well that is all there is to it. Enjoy.
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
•
50117 visitors to this page since
6 Oct 05 •
|
|
|
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|