Using a nice loader can help keep guests entertained during the times when they will have to wait while your work is loading. Some good uses for the movieClipLoader class, or mcl, include loading thumbnails or images in a photo gallery or portfolio, or loading swfs that are used as new pages or content areas of your website.
The MovieClipLoader is a variable that will broadcast events to a listener object that you assign to the loader. This listener object has functions assigned to different event names. When the listener catches one of the broadcasted events that it has been assigned a function for (in other words: told to listen for), the assigned function will be executed.
To get started, insert a new symbol (ctrl + F8) and name it loader_mc. This will be the graphical half of your MovieClipLoader. Give loader_mc an instance name, in this case, just use loader_mc again. Inside loader_mc, create 3 layers, as in the picture below. On the top layer, create a circle. Give it a black outline to the thickness of your preference. Now select the inside of the circle and cut it (ctrl + x), be sure not to cut the outline, and then select the keyframe on your 2nd layer and paste it in place (ctrl + shift + v).

Select the black outline and press F8 to make it a new symbol. Choose a movieclip and name it “border_mc” - no quotes. Give the outline an instance name of “border_mc” as well. Again, no quotes.
Make the 2nd layer a mask of the 3rd (right click > mask). On the bottom, or 3rd layer, draw a square large enough to cover your circle. Give it a nice gradient, or colour and then turn it into a movieclip (F8). You may give it any name you like, I named mine square_mc (because it’s a square and I turned it into a movie clip). Once you have turned the square into a movieclip, give that instance of the square an instance name, I used grad_mc (for gradient, and mc so in the code I know what type of element it is).
Next step is to make a dynamic text field in a new layer above “black_outline”. In the text field just type 100% to make sure the characters fit so you don’t get any word wrap with your numbers. This can be controlled through code, but for sake of simplicity and clean code, we won’t do that this time. Give this text field an instance name of “loader_txt” - no quotes.
Final step of the design portion of this is to embed the fonts used in your text field (loader_txt). To do this select the text field and find the embed button. Once this has been pressed a little window should come up. Select “Numerals” and “Punctuation”, then press “OK”. This step is critical, without it, your text may not show up when you compile your movieclip. You only need to include punctuation and numerals in your embed, because your text field is only going to be showing the loading percent, so numbers, and if you desire, the % sign. In the picture below, I have the fonts bolded, this caused some problems when embedding the text. If you have bold text and it does not show up, try removing the bold.

Right now you should have one movieclip created with the components inside. Everything is now ready for the actionscript.
Go back to your root level (Scene 1) and create a new layer for your actionscript. Give the layer the name AS or actions and lock it. We lock it so we don’t accidentally place elements on the code layer. In that code layer, add the code below. The code looks very long due to the fact that I put many comments in to explain it to you. It’s really not as bad as it looks!
-
// Stop action to prevent the playhead from moving on.
-
stop();// Import classes for tweening
-
import mx.transitions.Tween;
-
import mx.transitions.easing.*;
-
-
// Declare the listener object
-
var oListener:Object = new Object();
-
-
/* Assign a value to "r" for root. Functions executed
-
from an object (like we will be doing) lose the scope
-
of ‘this’. I prefer using ‘this’ rather than ‘_root’
-
because if I were to bring this code into another .fla,
-
I would not be able to garuntee that ‘_root’ will be what
-
I need it to be. It is also helpful to assign values for
-
the loader_mc, and the textfield inside the loader.
-
*/
-
oListener.r = this;
-
oListener.loaderMc = this.loader_mc;
-
oListener.loaderTxt = this.loader_mc.loader_txt;
-
oListener.gradMc = this.loader_mc.grad_mc;
-
oListener.borderMc = this.loader_mc.border_mc;
-
-
/* Assign a function to the first event to listen for
-
onLoadStart gets broadcast by the movieclip loader
-
when the specified has begun to be loaded.
-
*/
-
oListener.onLoadStart = function(targetMC:MovieClip):Void{
-
// For information on what this tween means, check out the tutorial on tweening.
-
// at www.SanctifiedStudios.com
-
var loadAlphaTween:Tween = new Tween(this.loaderMc, "_alpha", Strong.easeOut, 0, 100, 2, true);
-
}
-
-
/* onLoadProgress gets broadcast by the movieclip loader
-
when download progress has been made on the target.
-
*/
-
oListener.onLoadProgress = function(targetMC:MovieClip, nLBytes:Number, nTBytes:Number):Void{
-
// nLBytes is loaded bytes, nTBytes is total bytes to be loaded.
-
-
/* Declare a new variable nPer (the n is the variable type. n is for Number) the Per
-
stands for percent.
-
To find the percent loaded, you need to divide the loaded bytes by the total bytes.
-
We then need to multiply it by 100, becuase this will return a decimal value. To
-
finish it off, we round it. This prevents us from getting large ammounts of decimal
-
places.
-
*/
-
var nPer:Number = Math.round((nLBytes/nTBytes)*100);
-
// This refers to the oListener object, where we assigned the values for the loader mc
-
// as well as the textfield that we had in the loader.
-
this.loaderTxt.text = nPer + "%"; // With the % sign.
-
-
/* remove this line to remove the % sign on your loader
-
-
this.loaderTxt.text = nPer; // Without the % sign.
-
-
remove this line as well*/
-
-
/* Time to move the gradient up!
-
-
The Math:
-
The _y property is the y axis (up and down) value.
-
-
"this.borderMc._height * (nPer/100)"
-
This line finds the relative location to put the gradient clip.
-
So if the loader has loaded 50%, it would find the pixel at the 50% point of the
-
loader circle. For example, if our loader is 80 pixels high, and 43% of the target
-
has been loaded, we would put the gradient at 80 * (43/100), which = 34.4
-
-
We add the "this.borderMc._height - " to the start of the equation to make the
-
gradient appear at the bottom and move up. Without that part on the start of the code
-
the gradient will move from the top down.
-
*/
-
this.gradMc._y = this.borderMc._height - (this.borderMc._height * (nPer/100));
-
}
-
-
/* The final event to listen for is the onLoadInit. This is when the item
-
has been fully loaded and initialized on the stage.
-
*/
-
oListener.onLoadInit = function(targetMC:MovieClip):Void{
-
// Just some animations to get rid of the loader and transition the image on
-
var loaderTween:Tween = new Tween(this.loaderMc, "_alpha", Strong.easeOut, 100, 0, 1.5, true);
-
var targetTween:Tween = new Tween(targetMC, "_alpha", Strong.easeOut, 0, 100, 1.5, true);
-
}
-
-
/* Create the moviecliploader variable */
-
var mcl:MovieClipLoader = new MovieClipLoader();
-
-
/* Assign the listener we made above to the moviecliploader. When the mcl broadcasts events
-
the listener will now pick them up and react.
-
*/
-
mcl.addListener(oListener);
-
-
/* Create a new movieclip to load the image into, and then tell the mcl to load an image.
-
The first parameter is the path to what you are loading, the second is the target mc
-
to load into. Example: mcl.loadClip(path, target_toLoadIn);
-
*/
-
this.createEmptyMovieClip("imageHolder_mc", this.getNextHighestDepth()); // creates an empty mc
-
-
// step2.gif is an image I have local to my fla. Replace this with any image you desire.
-
// The mc we just made above will be our target. It resides at 0,0 on the stage by default.
-
mcl.loadClip("step2.gif", this.imageHolder_mc);













Subscribe by Email
2 responses so far
1 John // May 17, 2007 at 6:50 am
Thanks I needed this.
2 hb // Sep 7, 2007 at 2:36 pm
the code didn’t help but the picture did!!!!!
Leave a Comment