Actionscript 3 External Preloader

A preloader is an important part of any sizeable Flash app as you probably don’t want your users to see nothing while the app’s loading. There are two types of preloaders that you can employ: external and internal. In this tutorial we’ll see how to implement External Preloaders, which load the contents from a separate file. This method is much cleaner and easier than the internal way, so I recommend you use it whenever you can. It also allows you to keep your preloader and main content independent of each other.

The general idea is to set up a Loader object in your preloader SWF, and make it execute 2 functions: an update function, and a finish function. The update function is called every time load progress is made, and the finish function is called when loading is done. You would accomplish this through Event Listeners as follows:

var request:URLRequest = new URLRequest("Content.swf");
var loader:Loader = new Loader();

loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);

What you do inside those functions is totally up to you. If you have a loading animation that animates relatively to the progress (like a cup filling up), then you would update that animation inside the loadProgress function. But as an example, we’ll be updating a percent counter as the load progresses.

function loadProgress(event:ProgressEvent):void
{
    var percentLoaded:Number = event.bytesLoaded / event.bytesTotal;
    percentLoaded = Math.round(percentLoaded * 100);

    this.percentLoaded.text = String(uint(percentLoaded)) + "%";
}

function loadComplete(event:Event):void
{
    trace("Load Complete");
}

To wrap it all up, we call load on the URL request, and add the Loader object to the preloader’s Display List, so that the content shows up when it’s done loading.

loader.load(request);
this.addChild(loader);

Also, make sure that the preloader’s dimension and frame rate settings are the same as the main content’s.

Here are the source files with the external preloader, and a test content SWF.

External Preloader

Test content SWF

Tags: ,

54 Responses to “Actionscript 3 External Preloader”

  1. cybereality says:

    Thanks a bunch man.

    For some reason they made preloading unnecessarily difficult in AS3.0. Even the Adobe LiveDocs shows some weird way that takes like 3 pages of code and doesn’t even work right. I knew there was a simple method. Thanks again.

  2. escee says:

    hah, i donno who you are, just found your site on a google search for “AS3 swf preloader” , but thanks a bunch, this is perfectly simple.

  3. Steve says:

    Thanks a lot! It’s easy to follow your tutorial.

  4. The Reaper says:

    Thanks man! Keep up the clean work you have!

  5. Ian says:

    Hi. I am absolutely NOT a competent Flash or ActionScript person. In fact I try to stick to what I do best, which is making corporate movies. However, as a freelancer I have to get involved in things like the design of the website, hence I found myself looking for a simple to use preloader for my Flash-only site. I kind of understand the principles involved in your code but I can’t seem to get it to work on my site :-( The orb rotates a few times then the screen goes blank and I have the same wait as before until the movie loads. All I did was to change the content.swf variable to videoit.swf (my movie), then placed the externalpreloader.swf and .html files in my home directory. Where am I going wrong? Thanks for any help or hints you can give.

  6. Laura says:

    Thank you so much for posting this tutorial. I am updating a very long program from AS2 to AS3 because I think it will preform better, but it has been difficult because I have so many tricks up my sleeve related to AS2. This tutorial helped a lot with at least one of my problems.

  7. Jeff says:

    Thank you for this preloader. I use the Preloader mc in my Intro to Flash final project. One question: percentLoaded shows up as an ActionScript word (shows up blue) even though you use it as a var name. Is there any conflict possible there?

  8. Nikola says:

    Thank you so much for posting this tutorial. This is perfectly simple.

  9. waffle says:

    This is the best example of an external preloader I’ve seen so far. It works wonderfully! Thank you!

  10. andyK says:

    Hey, thanks mate great tutorial

    I’m currently searching for a way around this problem im experiencing with a preloader

    Trying to add a ‘shuffle’ type music player to my website.
    Works fine but doesn’t load with the main preloader

    heres the code im using:

    var randomSong:String = new String(“music/track” + (Math.ceil(Math.random() * 40)) + “.mp3″);
    var songtime:URLRequest = new URLRequest(randomSong);
    var thisSound:Sound = new Sound();
    thisSound.load(songtime);
    thisSound.play();

    Is there a way I can include this random tune selection within the preloader. Hope ive given you enough details, im nackered :)

    any help would be great thanks

    xx

  11. andyK says:

    .: above solved :.

    thanx x

  12. frank says:

    …my very durating search has an end! I love you! Thanx very much!

  13. Jflo says:

    Thanks for posting this. It works for the most part for me, but I noticed my percentLoaded text only reaches about 82% before starting the loaded swf. I am loading some audio from my library within the loaded swf. Could this be the cause? Any ideas on how to correct this?

  14. Jflo says:

    Oh, and I am also using TweenLite to fade out one of the audio clips. Not sure if that makes any difference…?

  15. John says:

    I totally owe you a beer now.

  16. Doogog says:

    Hey John,

    Did you get it working?

  17. palksueto says:

    hi, i have a preloading external swf files from my main.swf in localhost works fine but when i upload to remote server, it doesnt work properly, what is the problem?, maybe a security issue by As3

  18. Swedisk Monkey says:

    i was very sceptical i had tried a VERY similar script at gotoandlearn.com, getting mysterious #1034 errors and almost given up. this worked, so thanks for saving my day (and night)!

  19. James says:

    The preloader works, just when it gets to loading my own .swf that I’ve made I get “Error #1009: Cannot access a property or method of a null object reference.” I have everything in the same directory and the .swf works on its own. Any clue here? Thanks…

  20. Arunava says:

    Just used your preloader. It simple rocks..Thanks a ton :)

  21. Flash Designer says:

    hello,

    thanks that great, but how I use it for 3 swf file

    thanks again

  22. Patrick says:

    This is great, you are a life saver. So simple and painless.

  23. Frank says:

    Is it possible to make two classes: Loader class and Preloader class, and then display the progress data in a textField, from the Loader Class, in the Preloader class with a custom Event? Or does the progress data have to travel to long? Thanx

  24. Vince Delmonte says:

    This is very up-to-date information. I’ll share it on Digg.

  25. purvi says:

    This is really cool! It works for the most part for me, but I noticed my percentLoaded text only reaches about 30% before starting the loaded swf. can you help me go till 100% before loading the content.swf.

    Thanks in advance.

  26. moldy says:

    i get the same bug purvi, I notice it happens only when i move the x or and y of the txt field

  27. Dave Nicosia says:

    Thanks! You saved me!!!

  28. Dave Nicosia says:

    Sorry…I mistyped my website in my other comment…

  29. M.P. says:

    I can’t believe how simple this is. I been trying to figure this out for months. Thank you so very much.

  30. sachin says:

    hi there, thanks for the code, but i am facing a problem , i am going wrong somewhere. the external swf that i ‘m calling is nodoubt loading but it loads when the loader is showing 20-30 % of the loading time. my question is is it really calculating because if so it should show 100 % just before it loads.

  31. Lisa says:

    This works great! I have a question. How would I modify the loader to preload multiple swfs? I’m a flash noob and can’t figure this out for the life of me.

    Thanks!

  32. Doogog says:

    Hey Lisa,

    What you could do is to have an array of loaders set up the way I’ve outlined in the tutorial. Then on each loader’s loadComplete event listener, you increment a counter variable, and check to see if that variable is equal to the length of the array. When they are equal, you know you have loaded all of your contents, and can begin to add them all to the main display object.

    Let me know if this helps!

  33. Jackrabbit says:

    Is it just me, or does the displayed percentage loaded NOT match up with the ACTUAL percent loaded. I have the simulated download set to DSL speeds, and right around 30 or so percent, the flash is fully loaded and fades in over the preloader. Is there a way to sync them up better so the percentage preloaded is accurate?

  34. Doogog says:

    Hey Jackrabbit,

    The displayed percentage should definitely match up with the actual percent loaded. If you are getting a fully loaded at 30%, you may be calculating the percentage incorrectly, or getting the bytes data from a different place. Are you using the tutorial’s code as is, if not, could you send me a snippet of what you have?

  35. Jackrabbit says:

    Check this out. I’ve got the .fla’s there and the .swf example link. I used your code with only minor cosmetic name changes and still the .swf loader earlier than the preloader finishes. You’ll see in the .fla some commented out code I used trying out a similar loader from another forum.

    http://www.redbandtrailer.net/demo/indexDEMO.htm

  36. Jackrabbit says:

    sorry, this is the correct link…

    http://www.redbandtrailer.net/indexDEMO.htm

  37. Waffle says:

    Great tutorial! I was just wondering how you can specify where in the main flash file the external flash file will load.

    I’m planning on having a smaller external flash file appear at the bottom part of the main flash file.

  38. Anonymous says:

    Hey Waffle,

    Once you’ve loaded the content. You can use the loader just as you use any other DisplayObject. So to change its position, you would do:

    loader.x = 100;
    loader.y = 100;

  39. Maria Robertson says:

    Thank you very much, I have tried a couple of different types of loaders and when they were within the same movie, they seems to conflict with the code I have in the main movie. This works perfectly by having the movie as an external .swf and it is very adaptable. Brilliant.

  40. Anonymous says:

    Hey Maria,

    I’m glad to see that it helped!

  41. Waqas says:

    I can’t believe how simple this is. I been trying to figure this out for months.

    Thanks a lot!

  42. Web designing says:

    Thanks for posting this. It works for the most part for me, but I noticed my percentLoaded text only reaches about 82% before starting the loaded swf.

    Many thanks!

  43. Anonymous says:

    Hey Web designing,

    You might be seeing only 82% before starting the loaded swf because when testing locally, the download rate is enormous since it’s not over the internet.

    Since the percent is updated per frame, your swf could be loaded from 82% to 100% within a single frame, therefore it’ll never display 100%.

    To see it slow down, set your simulated download speed to something low like 56k in your Bandwidth Profiler when you’re testing the swf.

  44. Shane says:

    Thanks! Moving from AS2 to AS3 can be annoying at times. This was simple and easy to modify for future use.

  45. Anonymous says:

    thnx

  46. Anonymous says:

    this preloader is a HOAX!!! it does not work at all

  47. Stefan says:

    Hi, i have just tested your FLA and when I put one of my swfs to load, it displays part of the content before the percentage has reached 100%. I am wondering if this has something to do with my external swfs?

  48. Stefan says:

    Never mind.. i accessed the external swf timeline and simply put gotoAndPlay(1) as soon as the loading is complete, now it runs okay.

  49. Chad says:

    Hey thanks for that. I made a little animation in the preloader swf but it doesnt show up in the browser, only the test window. I’m opening the preloaders html file as the index.html , what am i doing wrong??

  50. clarissah says:

    have you guys actually tested this on a browser in a live server?
    on IE it sits at 0 while on firefox it goes straight to 100

  51. saijin says:

    This is useless.. It is not working!!

  52. NicolaRolla says:

    @Clarissah: Mine did the same thing. Just make sure in the actionscript you have .swf and not .html.

  53. Vernon Joyce says:

    Quick question, after using a preloader that loads an external swf file, I uploaded both swf files which was preloader.swf and website.swf. So then, website.swf is supposed to enter the stage when my loader gets to a 100. However, the loader swf stays open and website.swf doesn’t open. What am i doing wrong?

  54. Gatwick Airport Taxi says:

    Greetings,

    I made a little animation in the preloader swf but it doesnt show up in the browser, only the test window, maybe I am doing something wrong?

    Regards,
    Jania

Leave a Reply