Preloader in Flash CS3 - Actionscript 3.0 way
When you want to preload a SWF, JPG,PNG, or GIF in Flash CS3 using Actionscript 3.0 you must use some slightly different way as you did in Actionscript 2.0. The MovieClipLoader class is not used anymore in AS3. Also the loadVars classes are away in Actionscript 3. In this small article I discuss the new way of preloading with a simple example that preloads an image.
I am using the Adobe Flash 9 public Alpha (actionscript preview) for this example which you can download here at the labs.adobe.com site.
Let’s start to have a look at the first lines of the code which is all placed on the first frame of a new Flash 9 document.
All loading stuff is done by the Loader and the LoaderInfo class so let’s import it:
import flash.display.Loader; import flash.display.LoaderInfo;
As you see they are located in the flash.display package. We make an instance of the Loader class:
var myLoader:Loader = new Loader();
The Loader class has a method called load which takes an URLRequest as parameter. So if we want to load something we must first define an URLRequest instance that points to the file we want to load:
var myRequest:URLRequest =
new URLRequest("myImageToLoad.jpg");
To use this URLRequest class we also must import it; so at the top of our code we put following code under the import of the LoaderInfo class:
import flash.net.URLRequest;
Now we can use the load method of the Loader class to start load an image on stage and use the URLRequest as parameter:
myLoader.load(myRequest);
Next step is to capture the progress of the loading. This is done by adding eventListeners to the contentLoaderInfo property of the loader object we have instantiated. In this case we use the Event.OPEN, ProgressEvent.PROGRESS and Event.COMPLETE events and we also point to a callback function we want to use:
myLoader.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader); myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress); myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoadResult);
As you see we declared three callback functions: showPreloader(), showProgress and showLoadResult. I used the Event constants to describe which event I want to listen for. This implies that we must import these necessary classes so Flash can interprete those specific constants. So at the top of your code write following code:
import flash.events.Event; import flash.events.ProgressEvent;
What we want to do is showing the load progress in a simple textField. But offcourse you can make your own custom preloader if you want; principle is the same. So far so good, thus we need a textfield, lets instantiate one:
var loadProgress_txt:TextField = new TextField();
Again, if we want to use the TextField class we must import it: at the top of your code place following import statement:
import flash.text.TextField;
Ok, now we are ready to write the first callback function that reacts on the OPEN event. In this function we just want to display the textfield. We do this by simply adding it to the displayList:
function showPreloader(evt:Event):void {
addChild(loadProgress_txt);
}
The second callback function is called when progress during loading is made. In this function we want to show how much is already loaded:
function showProgress(evt:ProgressEvent):void {
loadProgress_txt.text = “loaded:”+evt.bytesLoaded+” from “+evt.bytesTotal;
}
As you see we can get the bytesLoaded and the bytesTotal propertys directly from the progressEvent. We override the text property of our textField every time progress is made.
The last callback function we must write is the showLoadResult() function. This function only must remove the textField from the displayList and add the content that is loaded to the displayList : so here are the last lines of the code:
function showLoadResult(evt:Event):void {
removeChild(loadProgress_txt);
addChild(myLoader);
}
As you see preloading in AS is a bit different and you have to get used to the new way but ones you get it, it’s fun!
That’s it for now; if you have questions… please leave a comment !
Filed under: Flash


















Nice! Saves me (and other readers) the hassle of researching it.
Would this also be used if you had to preload FLVs?
Thanks, mate!
It was really helpful! I’ve been struggling to update some of my old AS 2.0 code!
Cheers!
Gus
@ Bramus: Nice to see you here mate !
@ nomaeswonk: You can use bytesLoaded and bytesTotal for that (flash.net.NetStream class ) ;-).
@ Gus thanx! Have fun playing AS 3.0
Is it Me - or does Importing every class before it can be used seem just retarded?
hi,
does somebodu have an idea how to implement a preloader in cs3/as3 that loads the main movie, i.e. the movie that the preloader is in?
You would still have to export all linked assets into a frame different than frame 1 to not render a preloader useless, same situation with Flash CS3! But how do you write a simple preloader in frame 1 that uses the Loader Class?
@ sascha/hdrs : Because the instance of the main class of the SWF file has no Loader object, the loaderInfo property is the only way to access the LoaderInfo for the instance of the main class of the SWF file.
Keep getting this error
ReferenceError: Error #1065: Variable request is not defined.
This is my code:
stop();
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.events.Event;
import flash.events.ProgressEvent;
var myLoader:Loader = new Loader();
var myRequest:URLRequest = new URLRequest(”2007_initgroup_home.swf”);
var loadProgress_txt:TextField = new TextField();
myLoader.load(request);
myLoader.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoadResult);
function showPreloader(evt:Event):void {
addChild(loadProgress_txt);
}
function showProgress(evt:ProgressEvent):void {
loadProgress_txt.text = “loaded:”+evt.bytesLoaded+” from “+evt.bytesTotal;
}
function showLoadResult(evt:Event):void {
removeChild(loadProgress_txt);
addChild(myLoader);
}
You get that error because your variable request is not defined, you are using it in this line
myLoader.load(request);
and a few lines before you declare your URLRequest variable as myRequest, just change that line for this
myLoader.load(myRequest);
thanx Ponsho, you are my hero ! I corrected the error in the code.
is there any way to pass the properties of the button to the preLoader classes? For example I am loading multiple thumbnails but I want all the thumbnails to have their own preloading bars positioned over themselves.
I am loading one movie after another, and I can get the first movie to hide, but the sound still plays. How do I completely unload the movie?
Hmm….AS3 seems more convoluted! import this and import that just to get some simple things done. Not sure I like it. I’m not an expert programmer and classes sort of snag my brain a little bit but this seems rediculous.
Buddy
Does loading a Flash 8 movie into a cs3 movie causes any problem in the embedded flash 8 movie? like some stuff inside might not work anymore?
@ keneth : yeah that’s indeed true. There is a hack around changing the bytecode at runtime when loading… have a look at : http://www.danielhai.com/blog/?p=35
Thanks for taking the time to post this, it really did help a lot
Thanks for this, it was really useful for me
@Alison, Marie, I want to give back to the community so it was a great pleasure to write this article !
Extremely helpful!! Thank you for saving me hours of research!!
am i the only one getting an 1093 syntax error for this code?
function showProgress(evt:ProgressEvent):void {
loadProgress_txt.text = “loaded:”+evt.bytesLoaded+” from “+evt.bytesTotal;
}
Is there a way of converting this so it loads the whole movie that the preloader is in instead of just one picture?
cw: If you copied and pasted the text from “gazza” you will need to re-type the ” signs
@Chris : take a look at the extra preloading article which you can find at http://newmovieclip.wordpress.com/2007/04/30/more-about-preloading-in-flash-cs3/
There we preload the whole movie !
Is it true this way I can run an external swf, therefore preloading all objects in the library and on the timeline, unlike the “extra preloading article”??
Also another question I have is the bytes loaded stays on 0, any ideas?
Awesome !!!!!!
Now this is what I call vivid information….
Hats OFF man keep up the good work….
Does anyone know how to load multiple swf one after another?
Does anyone know how to load multiple external swfs’ one after another?
Raffi use an array of loader objects. Each one will handle/load one external swf.
Thanks for the script, the first one what was guiding me to good results!
There’s only one problem:
I’m loading a swf file with your preloader, but after finishing, movie is not starting with the first frame. It looks like it is started few seconds before preloader has finished.
I’m really new with Flash Actionscript, so do you have any idea what could be wrong?
Thanks for the info, Parag!
hello,
i am complete new to AS at all.
does this script also load .flv movies?
get the message
Error #2044: IOErrorEvent unverarbeitet. text=Error #2124: Die geladene Datei weist einen unbekannten Typ auf.
(unknow typ)
how can i then adapt this to continue to the 2nd frame, where the website realy starts?
Thanks very helpful. Straight to the point. I still don’t get why they don’t include clear examples like this in with the help files!!!
Hi -
This is great - thanks for posting it. I am slowly learning AS 3.0
One question: This example still seems to work even when I comment out the lines that import the various classes (at the top of the code). So, why then are they needed?
Thanks,
Carl
i’m getting this error…
1093: Syntax error.
loadProgress_txt.text = “loaded:”+evt.bytesLoaded+” from “+evt.bytesTotal;
also ‘1078: Label must be a simple identifier’ for line 24 as shown above.
:O
Im getting the same as konshuss… any recommendations?
figured it out
konshuss if you copy and pasted all the code, make sure you retype the quotation marks 
Thank you, Koen. Very generous of you to do this! It works great except I had to adjust the text field (it wasn’t displyaing the loading numbers completely. it was like the text field wasn’t wide enough ) so I had to add this to your code just beneath the var loadProgress_txt:TextField = new TextField();
loadProgress_txt.x = 0;
loadProgress_txt.y = 0;
loadProgress_txt.autoSize = TextFieldAutoSize.LEFT;
(this puts the progress text in the upper left corner. you can adjust the x and y to your needs)
Hope that helps anyone with the same issue!
I’m getting this:
Error #2044: Unhandled IOErrorEvent:. text=Error #2036: Load Never Completed.
Am I doing anything wrong? my code:
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.text.TextField;
var myLoader:Loader = new Loader();
var myRequest:URLRequest = new URLRequest(”loaded.jpg”);
myLoader.load(myRequest);
myLoader.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoadResult);
var loadProgress_txt:TextField = new TextField();
loadProgress_txt.x = 0;
loadProgress_txt.y = 0;
loadProgress_txt.autoSize = TextFieldAutoSize.LEFT;
function showPreloader(evt:Event):void {
addChild(loadProgress_txt);
}
function showProgress(evt:ProgressEvent):void {
loadProgress_txt.text = “loaded:”+evt.bytesLoaded+” from “+evt.bytesTotal;
}
function showLoadResult(evt:Event):void {
removeChild(loadProgress_txt);
addChild(myLoader);
}
Thank you again for this tutorial!
Hi,
Is there a way to get the size of an image to be loaded without actually loading it? I’m creating a gallery of images, with a Loader for each image (I created a class with all the necessary info for each image, including url, loader, etc…
and I want to be able to show the progress of loading the entire gallery as x% of total. Thanks in advance!
J
Thank you for the pre_loader code. I learned so much from that.
Regarding Ronny’s question:
Yes indeed, movies starts as soon it is initialized
and before contentLoaderInfo’s Event.COMPLETE is raised.
The obvious thing you could do is to put stop() command in the first frame of loaded swf.
More elegant solution, allowing that swf to start normally when run without preloader is to do the following:
myLoader.contentLoaderInfo.addEventListener(Event.INIT ,initHandler);
function initHandler(e:Event) {
MovieClip(LoaderInfo(e.target).content).stop();
}
Don’t forget:
function showContent(event:Event):void {
removeChild(myPreloader);
addChild(myLoader);
MovieClip(LoaderInfo(event.target).content).play();
}
at the end to start playing the loaded movie once MovieClip(LoaderInfo(event.target).content).stop(); has been called.
Thanks! I save lot of time,
[...] 1) http://newmovieclip.wordpress.com/2007/03/14/preloader-in-flash-cs3-actionscript-30-way [...]
Nice code
Thx a lot! Will save me some time for my flash project :p
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
var myLoader:Loader = new Loader();
var myUrlReq:URLRequest = new URLRequest(”image_effect.swf”);
myLoader.load(myUrlReq);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(event:Event):void
as Class
{
var rollover_animation:Class = event.target.applicationDomain.getDefinition(”rollover_animation”
var myMovieClip:MovieClip = new rollover_animation() as MovieClip;
addChild(myMovieClip);
}
Plz check for me, I have errors “1120 Access of undefined property myLoader”
@Mars, I tested your code here and it’s working for me… strange..if you send me your files, I will have a look at it..
Im getting syntax errors… whats up here? Could someone maby post a working code?
stop();
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.events.Event;
import flash.events.ProgressEvent;
var myLoader:Loader = new Loader();
var myRequest:URLRequest = new URLRequest(”flash.swf”);
var loadProgress_txt:TextField = new TextField();
myLoader.load(request);
myLoader.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,showLoadResult);
function showPreloader(evt:Event):void {
addChild(loadProgress_txt);
}
function showProgress(evt:ProgressEvent):void {
loadProgress_txt.text = “loaded:”+evt.bytesLoaded+” from “+evt.bytesTotal;
}
function showLoadResult(evt:Event):void {
removeChild(loadProgress_txt);
addChild(myLoader);
}
Great info! Works perfect!
Great job man!
Help me a lot!!
Thnx