New video tutorial on ActionScript 3 preloading

I just finished uploading a new tutorial that shows you the cleanest, most reliable way of preloading your ActionScript 3 movies. Like most things in Flash, there a many different ways of preloading your main content file. This approach takes a lot of the guesswork out of it and instead lets you focus more time on actually creating your movie, rather than worrying how it will preload. Check it out at http://www.gotoandlearn.com.

Lee

Comments

  1. September 15th, 2008 | 1:17 pm

    Nice video

    Shouldn’t you remove the listeners for the PROGRESS and COMPLETE events once the COMPLETE event has fired?

  2. lee
    September 15th, 2008 | 1:26 pm

    @zedia Basically I only remove event listeners when I want an object to be garbage collected. The Loader instance actually contains the main movie. So if I wanted the loader to be picked up by the GC I would remove listners, references, etc. This of course will all be easier once we have Loader.unloadAndStop in Flash Player 10.

  3. September 15th, 2008 | 1:33 pm

    I might be wrong but I tought having a lot of listeners in your flash movie could make it play slow, and since those listeners(PROGRESS and COMPLETE) are never going to be used again, I tought removing them would be the cleanest way to do it.

  4. lee
    September 15th, 2008 | 1:46 pm

    @zedia Yes you’re absolutely right. You always want to remove event listeners for objects you are no longer using so they can be properly garbage collected. In this case you could remove the progress and complete events but it wouldn’t be necessary from a GC perspective unless you wanted to remove the Loader.

  5. September 15th, 2008 | 1:55 pm

    nice job–definitely the easiest way to deal with this stuff. Another minor point, however, you might include a cache buster…considering you’ll be changing that content.swf a lot–you want to look at the latest… something like:

    var url:String = “content.swf”;

    if ( stage.loaderInfo.url.substr(0,4) != “file” ) {
    var d:Date = new Date();
    url += “?cachebuster=” + d.getTime();

    }
    l.load( new URLRequest( url) )

  6. September 15th, 2008 | 2:04 pm

    how would you modify this to load a sequence of swfs rather than just one, while displaying the aggregate percentage of all swfs? not looking for code, just theory.

  7. lee
    September 15th, 2008 | 2:10 pm

    @Phillip Great point! Forgot all about the cache aspect.

    @michaelwilson Basically you would load a series of Loaders on after the other and wait until the last one loads before adding to the display list. So in the complete event for the first Loader you could then start the second one, etc.

  8. September 15th, 2008 | 2:23 pm

    OK, let’s say we don’t care about GC, like in this case, we don’t want the loader to be garbage collected. On the CPU level, is it better to remove listeners after they have been used (like in this case they will never be used again) or it doesn’t make much difference and you can just leave them as is?

  9. September 15th, 2008 | 2:29 pm

    Nice tut lee. I actually did that same kind of thing but separated them on frames. This way is much better though. Maybe a tut on loading fonts would be nice for flash since it is quite hard to find out how on the web.

    Also wanted to ask what software you use for creating these tutorials. I’m looking to create some video tuts to teach people how to use my classes.

    Keep up the good work. =)

  10. lee
    September 15th, 2008 | 2:32 pm

    @zedia If you’re sure that you won’t be loading anything again with it, it can’t hurt to remove those listeners. How much this will affect performance I’m not sure of though.

  11. lee
    September 15th, 2008 | 2:36 pm

    @Clemente I use iShowU on the Mac and Camtasia on the PC.

  12. September 16th, 2008 | 12:03 am

    As usual a great tutorial!

    Can this, with some modifications, be applied to Flex as well? Flex doesn’t have a timeline but maybe it could somehow be bypassed.

    Thanks!

  13. September 16th, 2008 | 1:26 am

    “how would you modify this to load a sequence of swfs rather than just one, while displaying the aggregate percentage of all swfs? not looking for code, just theory.”

    Michael what I would do, is set up a new Array(); that’s empty, then list all of my swifs in strings. I dont do timeline coding so , I would also use a document class, Lee got a good tutorial on that. Anyway so in the constructor I would basiclly set up some Strings containg URL to my swiffs and do a init() function wich does this.

    Push each into the array.

    Then just itterate trought the array,

    for each ( string in myArray)
    preload

    and maybe have a iteration var, called i. and when that iteration var is equal to the arrays length we know we preloaded all, then we can starta adding the content in the array to display list.

    and i would prolly do that the same way as i loaded it,
    for each ( swiff in myArray)
    add to display list

    and keep an iteration var there also.

    thats pretty just theory, but lets say your preloader is a fullscreen swif, and you’r swifs are all different dimensiones and maybe you got a sketch of it in photoshop and figure out that ,.. hey this swif is 200 by 100 and goes i top right corner.

    in my init() function where i push the strings to the array. I would rather push objects into the array.

    And what I/we want into these objects are the url path to the swif, x and y position.

    so just push the array

    myArray is a new Array wich contains {object} and it gots url: x: and y:
    and those are specific for each swif so….

    well that’s how i would go about it, and I think it would work. Did I make any sense ?

  14. September 16th, 2008 | 1:29 am

    ah forgot to mention with objects in the array before you add to display list, you set the position stored in each object as x, and y. So you have to iterate trough the array and “configurate” before you add to display list so, they turn up in the right spots.

  15. September 16th, 2008 | 2:54 am

    I think there is an easy way to do it “internal preloader”:
    convert all the components on the stage to a moiveClip and movie it to second frame
    in the library uncheck “export in first frame ” for all components
    go to file-publish setting -setting – on “export frame for classes” change it to 2
    then you can do internal preloader in first frame…

    by the way is it possible to load just the first frame until the user click to load the rest of frames is that possible in flash 10?

  16. September 16th, 2008 | 3:22 am

    Nice to see this in tutorial form (it’s the way I’ve been doing it for a year now!) but I just wanted to add one thing: The moment you add the loaded SWF to the stage it will start running.

    This isn’t always desirable. For example say you want to “fade out” your pre-loader or play a little “It’s loaded” animation before the main SWF starts, then you need to be a bit more careful when adding to the stage.

    You could cast the event.target to a LoaderInfo and then get the content from that (which is in effect your entire SWF). You’ve then a bit more control over when it’s added to the stage. Alternatively I usually do this but also make sure I have nothing in my main SWF documentClass constructor, but moving my constructor code to a “go” function. Then when my pre-loader is done, and the SWF is added properly, I call go() on it. But this is AS3, there are a hundred ways to skin this cat :) That’s just my preference.

  17. Wez Crozier
    September 16th, 2008 | 3:23 am

    When preloading AS3 movies, I often find it’s good practice to have Event.ADDED_TO_STAGE in the Constructor of the Document Class within the movie you are loading, to avoid problems with referencing objects that may not exist on the Stage yet, due to loading.

    Does this sound right? Or is it just not needed?

  18. September 16th, 2008 | 4:04 am

    hey lee

    your last tutorial does not apear in your tutorial feed
    thanks for the great tutorials
    keep on teachin

    andreas

  19. September 16th, 2008 | 4:54 am

    Personally I think the Frame metadata and AS 3.0 projects are the cleanest way to go. i.e. all content is within one swf, no need for loading external swf’s.

    http://www.bit-101.com/blog/?p=946

    @Marcus Andersson – this is the approach used in Flex.

    Its feasible this approach can be done with Flash by just throwing all the assets on frame 2 of your movie, with only the assets used for preloading on frame 1, then uncheck ‘Export of frame 1′ for the library assets.

    Wonder if this approach (and something that is relatively simple in an AS 3.0 project), has been resolved in the new Flash IDE.

    i.e. a “create preloader” button that enables u to pick the library assets you want to preload and takes care of this stuff for u.

  20. September 16th, 2008 | 5:16 am

    Just out of curiosity; why removeChildAt(0) instead of just removeChild(percent)?

    Also, agree about a tutorial on fonts in Flash, both embedding the right subset, using them in code and how to deal with the never ending annoyance of Flash’ inadequacy when it comes to calculating the dimentions of text in custom fonts. I get text fields with fonts that are cropped by the text field in some way all the time. Especially with the norwegian letter Å. it is an A with a ring above it, but very often it displays as A.

  21. September 16th, 2008 | 5:54 am

    [...] This videotutorial is really easy and shows a great way to preload a swf. Usually I used the two – frame method but this causes trouble (also explained in the tutorial). enjoy! http://theflashblog.com/?p=438 [...]

  22. Ben
    September 16th, 2008 | 6:11 am

    This approach seems really well suited if you’re able to work with multiple files, but what is the best approach for adding a preloader in AS3 if you want one self contained .swf. With the artwork, animation, audio and code all inside the same file? Say for a self contained game that needs to only be one file.

    Great tutorial, thanks for sharing.

  23. September 16th, 2008 | 7:43 am

    [...] New video tutorial on ActionScript 3 preloading (from The Flash Blog) [...]

  24. September 16th, 2008 | 7:57 am

    Nice tutorial as always. Big fan of gotoAndLearn…

    Do you see Adobe ever creating a clean way for us to preload our movies without multiple swf’s?

  25. lee
    September 16th, 2008 | 8:27 am

    @Tink Yeah there are specific problems that arise when using the Flash IDE. Preloading in pure AS3 is much simpler.

  26. lee
    September 16th, 2008 | 8:29 am

    @Øyvind Yeah you’re right, removeChild(percent) would also work. Not sure why I did removeChildAt(0) :-)

  27. September 16th, 2008 | 8:36 am

    Now here’s the big question: Is there anyway to pass FlashVars from the preloading swf to the main SWF?

  28. September 16th, 2008 | 8:36 am

    @Martin Lindelöf

    Take a look at our Library and LibraryManager

    @Ben, see my previous comment (i.e. stick all your assets on frame 2 that you want to preload).

    It will be disappointing if Adobe haven’t built something into the IDE for the next version of Flash, when it is such an easy solution.

  29. September 16th, 2008 | 9:15 am

    I have a class i’ve been working on called Prequel that simplifies and organizes preloading in AS3. its located on google code at:

    http://code.google.com/p/prequel-preloader/

    I’m currently at a .95 release, i believe it to be stable but I have just made modifications this week and have not had an opportunity to publicly test this release.

    Prequel features:

    loading of the container SWF

    loading of external files of any type, including those not handled by flash directly (like PDF’s for example)

    Aggregate percentage information for all files being loaded

    option to buffer media files to a specified to percentage

    return of objects loaded (XML, URLVars) in a ready to retrieve state at completion of the load

    Lightweight(under 4k)

    XML and URLVariables can be preloaded directly into objects ready for parsing when the COMPLETE event is fired.

    A custom ‘PROGRESS’ event returns data on the file being loaded, percentages, data values, and more.

    Please feel free to try this out and submit bugs or feature requests, hopefully that’ll get this to a 1.0 release.

    sample implementation:

    import com.jeffdePascale.prequel.*;

    var preload:Prequel = new Prequel();
    preload.addEventListener(Event.COMPLETE, eCompleteHandler);
    preload.addEventListener(PrequelEvent.PROGRESS, eProgressHandler);
    preload.preload([this, "file.swf", "test.mp3"]);

    function eCompleteHandler(e:Event):void{
    trace(“done”);
    }

    function eProgressHandler(e:PrequelEvent):void{
    trace(e.totalPercent);
    }

  30. lee
    September 16th, 2008 | 10:48 am

    @Ben Just make sure that your document class doesn’t reference things that you don’t want to load on frame 1. This includes any symbols on a any layer that has an instance name. This is precisely why IMO it is better to just use a preloader SWF. This way you don’t have to think about any of this nonsense. If you absolutely must have only one SWF then follow Colin’s recommendations in Essential ActionScript 3.

  31. lee
    September 16th, 2008 | 10:55 am

    So just to sum up based on the great comments so far. Here are the recommended approaches based on environment.

    Flash CS3 with no custom classes or components
    This can be done old school by simply moving your main assets off of frame 1. See my Basic Flash Preloader tutorial for this.

    Flash CS3 with custom classes or components
    Use the method that I showed in this tutorial if you want the cleanest approach. You can also use Colin’s approach if you need a single SWF file.

    Pure ActionScript project compiling with MXMLC
    Check out Tink’s approach in comment 19. Sounds like that is the best way to go.

    Flex Framework project
    The framework takes care of everything for you.

  32. September 16th, 2008 | 12:00 pm

    @lee
    Thanks for the insight on using the onComplete event fired by the loader class to start the next… i kinda figured that one… my real concern was how to aggregate the bytes loaded content.

    @Jeff dePascale
    Thanks for your prequel code. I’ll look to see how you handled the aggregate total bytes loaded info… that’s exactly what i’m looking for.

  33. September 16th, 2008 | 1:35 pm

    @michael

    The way it works is a two pass approach – first it goes through and checks the bytesTotal for each file being loaded, calculates in the mediaBuffer if necessary, adds the value to the multidimensional array for each item and to the overall total, then goes back and does the actual load from step one of the file array again. Hope that helps.

  34. September 17th, 2008 | 2:44 am

    Lee,
    Thanks for all this and the summing up
    >>
    Flash CS3 with custom classes or components
    Use the method that I showed in this tutorial if you want the cleanest approach. You can also use Colin’s approach if you need a single SWF file.
    >>
    I can’t help thinking that this has got a whole lot more complicated in the move from AS2 to AS3. Is this something which has happened “by design” for good reasons or is it just a by-product of fitting the AS3 VM into the existing IDE? If its a by-product does that mean we can look forward to it being simpler in future versions of the IDE?
    Given how frequently you need to make a preloader I like Tinks idea of a “Make this SWF preload itself” button. Failing that I would settle for a return the old school “put everything in frame 2″ which is guaranteed to work every time.

    Anyway cheers for clarifying it all

    Joe

  35. September 17th, 2008 | 8:41 am

    @Jeff dePascale
    … that’s exactly what I needed. Thx.

  36. Fedlarm
    September 18th, 2008 | 4:25 am

    @ Phillip Kerman in post 5

    There kan be some problems with adding the cache-buster as you suggests. If you create a projector file and run it local it won’t be able to get the file if you add an ‘?’ in the file name.

    Then off course it might not be necessary to add the cache buster. But still you might want to use the same swf for both online and offline.

  37. September 18th, 2008 | 8:38 am

    Great tutorial Lee.

  38. Rain
    September 18th, 2008 | 3:13 pm

    Hello Lee,

    I am a great fan of yours. Your all tutorials are really good. I just want to request you, please try to upload tutorials for carousel, dynamic addition of items if carousel, etc in AS3. I am really having tough time while converting as2 to AS3.

    I am new to flash and started with AS3 only so every time I go through the tutorials, I find difficulty with the actionscript.

    Thanks, your tutorials are great for new learners.

    Rain

  39. September 18th, 2008 | 7:01 pm

    [...] wanted to write this post for a very long time but Lee Brimelow beat me to it. But that is alright because he confirmed that the way I was doing my preloaders was a good [...]

  40. Mattias
    September 18th, 2008 | 11:10 pm

    How can I access the stage from within the loaded swf??? resize event handler???

  41. Nick Reffitt
    September 19th, 2008 | 12:00 am

    Hey, thanks for the video it was so helpful.

    I have been trying to put the preloader.swf into a web page, and it doesn’t seem to run at all, although it works fine when I run it normally.

    Can anyone help me out with this at all?

    Thanks a lot :)

  42. September 19th, 2008 | 12:57 am

    Serial coder from mauritius
    hi all,
    thats was really nice. i guess i like it more complicated.

  43. September 19th, 2008 | 1:01 am

    wot happens if you need to tranfer parameters from the preloader to the loaded swf, doesn’t that make this cake taste a little bit different.
    and anyway its just way to complicated dealing with two files instead of only one as it should be

  44. September 19th, 2008 | 3:31 am

    Flash CS3 with custom classes or components

    This can ALSO be done old school by simply moving your main assets off of frame 1 and exporting your classes and library assets on frame 2.

  45. lee
    September 19th, 2008 | 9:44 am

    @Tink Yeah that is the method Colin goes over in Essential ActionScript 3.

  46. KL
    September 20th, 2008 | 1:12 am

    @Fernand

    function onDone(e:Event)
    {
    var loadedSwf:* = e.target.content;//or l.content
    loadedSwf.param = “someParam”;
    }

    You can also call methods too.

    loadedSwf.doCall();

    Collins method is good too, but I’ve been using the technique described by lee @ work, but through a class, and this doesn’t cause harm. And there is nothing complicated about it.

    Nice one again lee.

  47. Marcus
    September 23rd, 2008 | 1:57 am

    Hi Lee,
    nice Tutorial! But I have much the same problem as Mattias (post 40). I always get this error when trying to access the stage properties in the loader.swf:
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at initStage()
    Any idea?

    Thanks!

  48. September 23rd, 2008 | 9:24 am

    Thanks for this tutorial. Very helpful.

    However, I’m having the same stage access issues as Marcus and Mattias.
    A tip from anyone on how to enable that would be greatly appreciated!

    Thank you!

  49. September 23rd, 2008 | 5:02 pm

    Found a solution to the stage access issue.

    In the Document class of the swf being loaded, empty the contents of the constructor function into another ( I called mine “init” ). In the constructor use

    addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);

    to call the init function when the loaded swf has been added to the stage (and therefore “stage” is accessible).
    Then init looks something like:

    function init(e:Event): void {
    // remove event listener if that is your thing
    // all the code that used to be in the Document class constructor
    }

    Hopefully this will help someone out!

  50. Travis
    September 23rd, 2008 | 7:37 pm

    Hey thanks Kirk for the solution but I am new to Actionscript 3.0 and I have a few questions about your solution. I have a swf and the document class is empty so would I just enter your code at the beginning of my script?

  51. September 23rd, 2008 | 11:01 pm

    Thanks for the tutorial Lee, if you have time I’d love to see a simliar one but for a animation based preloader instead of actionscripted numbers.

  52. September 24th, 2008 | 12:31 am

    hi all,
    again i like the teachings of lee, but i use document class in most of my projects cause i think the use of sprite is much nicer and less heavier that the old way of doing thing in flash and i clearly think that everybody should start using flex3. So i do use this technique anymore.

  53. Marcus
    September 24th, 2008 | 1:40 am

    @Kirk

    Thanks for the nice and easy solution :)

  54. Jeff
    September 24th, 2008 | 4:19 pm

    Anyone have an answer for Jared’s question at comment #27? (Flashvars) I want to load in a movie that uses flash vars from the page but it doesnt like the method outlined in the tut.

    Thanks!

  55. September 25th, 2008 | 3:02 am

    [...] The Flash Blog (Lee Brimelow): New video tutorial on ActionScript 3 preloading [...]

  56. October 5th, 2008 | 4:39 pm

    Great simple movie.

    I get an error with this, I’m using “full screen” flash on my “content.swf”

    I read somewhere that it’s due to using :

    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.dispatchEvent(new Event(Event.RESIZE));

    When the stage is not instantiated. Any workarounds?

    I get the error :
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at content_fla::MainTimeline/content_fla::frame1()

  57. Fraser
    October 6th, 2008 | 7:36 am

    Are there any settings that I should be aware of for the swf file that is getting loaded?

    I have tried a number of preloaders but with all of them, including this one, the file being loaded starts playing at about 45% (you can hear the music) and by the time the preloader finishes the movie has already played the first 8 seconds or so.

    Any suggestions?
    Thanks

  58. Tunç
    October 17th, 2008 | 2:46 pm

    @Kirk

    Thanx man. You saved my hours…

  59. October 28th, 2008 | 1:51 pm

    So here is the problem. I have started a AS Project in Flex 3 and with the Main.as as the loader and Content.as as the content. They are both extending the Sprite class and both are being compiled to SWFs with the Main.swf loading in the Content.swf. The problem is that if I reference ’stage.whatever’ in the Content.as on runtime it throws a ‘Error #1009: Cannot access a property or method of a null object reference.’ I am setting the stage properties in the Main.as like so;

    stage.frameRate = 30;
    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;

    I am pretty new to AS so I know it is probably and easy fix or maybe not. Help!

  60. October 29th, 2008 | 3:11 am

    Hi,

    thanks for the tutorial. I’m having a strange problem with on Mac though, especially Safari.

    After loading all the bytes (I traced the exact numbers), the onComplete function doesn’t fire. The swf basically just stops. Check it out here: ycchen.de/preloader.swf. The main application should appear when the block is filled with the brighter color, but on Safari nothing happens. On Windows it works fine.

    Any ideas? I’m basically using exactly the same construct shown in the video, except for the loading graphics.

    Would highly appreciate any help. Thanks.

  61. f
    November 7th, 2008 | 6:31 am

    @Kirk
    Thanks for the solution! you made my day!

    @Lee
    Thanks as always, i’m happy to know what the cleanest and easiest way for preloading is.

  62. November 7th, 2008 | 1:32 pm

    James O’Reilly his tutorial
    (http://www.adobe.com/devnet/flash/articles/liquid_gui_02.html)
    does not work with this preloader.

    The preloader does not count the percentage and it tells in the Output panel that there is a TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at index_fla::MainTimeline/index_fla::frame1()

    How can I make this so that it will work ? Could the cause be that
    inside the preloader is a part saying: PERCENT = null;
    and that there is a part in James his code saying: resizeHandler (null); ?

    Well thanks in advance.

  63. November 7th, 2008 | 1:50 pm

    never mind

  64. Simon
    November 9th, 2008 | 5:53 pm

    In response to Jared (#27) here is a workaround to get flashvars working:
    On the load complete part, put:
    newsrc = src.loader.getChildAt(0);
    src.addChild(newsrc);
    var flashvars:Object = {};
    for (var prop in root.loaderInfo.parameters) {
    flashvars[prop] = root.loaderInfo.parameters[prop];
    }
    newsrc.flashVars(flashvars);
    ——————-

    Just make sure you have a function called flashVars in the SWF you are loading in, and that it takes an object. Then you can play with them through there.

  65. November 26th, 2008 | 8:20 pm

    In response to a lot of the questions about the null reference errors with the stage, it appears that if you set stage parameters in the preloader, (scale mode etc) your loaded movie will not have any troubles.

    In my loaded movie I still set stage listeners and they don’t give me any grief.

    Here is what I do in my preloader (i like the preloader text to be centered so that is where the extra code comes in):

    stage.showDefaultContextMenu = false;
    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.align = StageAlign.TOP_LEFT;
    stage.addEventListener(Event.RESIZE, loaderResizeHandler,false,0,true);

    var _midStageX:uint = stage.stageWidth / 2;
    var _midStageY:uint = stage.stageHeight / 2;

    var l:Loader = new Loader();
    l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
    l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
    l.load(new URLRequest(“MainMovieToLoad.swf”));

    function loop(e:ProgressEvent):void
    {
    var perc:Number = e.bytesLoaded / e.bytesTotal;
    percent.text = Math.ceil(perc*100).toString() + ‘%’;
    }

    function done(e:Event):void
    {
    removeChildAt(0);
    percent = null;
    stage.removeEventListener(Event.RESIZE, loaderResizeHandler);
    addChild(l);
    }

    function loaderResizeHandler(event:Event):void {
    _midStageX = stage.stageWidth / 2;
    _midStageY = stage.stageHeight / 2;

    percent.x = _midStageX – (percent.width / 2);
    percent.y = _midStageY – (percent.height / 2);
    }

  66. kostas
    December 1st, 2008 | 12:25 pm

    Very nice tutorial lee.

    I have two questions to ask you.
    The first one is about if i can have the preloader in scene one and the main project in scene 2 of the same fla.
    The second question i would like to ask you is about the below issue:
    I made a document with four layers. In the fourth layer (at the first frame), i have a mc container in which i load data from a mysql database through amfphp. Everything works fine, but i don’t know how to make a preloader for this container, in order while the user is waiting the loading of the data, the container to display smth (as an animation clip).
    Could you please help me ?

  67. highwaykind
    December 4th, 2008 | 4:22 am

    Thanks for the excellent tutorial!
    My maincontent file contains external F4V files.
    Is there a way to get the preloader to preload those too?
    Thanks!

  68. george
    January 13th, 2009 | 1:58 am

    the preloader works fine on localhost but when i upload it, it doesnt work properly, what is the problem??

  69. Jon
    January 28th, 2009 | 2:48 am

    Hey guys!

    Yeh thnx Lee for the great tutes!!
    However, am having a similar problem to george (posted above). It’s very weird. When I test the movie (no simulation), it works all fine and the loaded SWf shows and everything, but when I simulate download, the loaded SWF doesn’t show… How can that be?… And when I upload and test online, it also doesn’t work. The “percent” text increases and you can see that the SWF is downloading but when it’s complete, it gets stuck there and the SWF doesn’t show (as if the “addChild” wasn’t working).

    any idea what’s happening? =[

    Thanks!

    jon.

  70. Christian
    February 6th, 2009 | 10:11 am

    @KL:

    Your solution for “transfering” FlashVars to the loaded SWF does not work like so:

    function onDone(e:Event)
    {
    var loadedSwf:* = e.target.content;//or l.content
    loadedSwf.param = “someParam”;
    }

    Use this code instead, it’s more universal also:

    for ( var prop:String in stage.loaderInfo.parameters ) {
    var loadedSwf:* = l.content;
    loadedSwf.loaderInfo.parameters[prop] = stage.loaderInfo.parameters[prop];
    }

    Best regards,
    Christian

  71. February 19th, 2009 | 7:55 pm

    I could not get it to preload a swf that uses a document class.

    For example, copy the preload.swf into a folder containing Lee’s Carousel 10 example. Rename Carousel.swf to content.swf and try running preload.swf

    I get “TypeError: Error #1009: Cannot access a property or method of a null object reference. at Carousel()”

    I can’t quite tell, but I dont think this is related to the problem Marcus etc had (post 49 etc).

  72. February 19th, 2009 | 8:00 pm

    So in summary, it seems to me that if you do one of the following three things, then its less hassle to just use Colin Mook’s all-in-one-swf method:

    1) Preload a swf that uses the document class.
    2) Pass FlashVars from the preloading swf to the main SWF.
    3) Access the stage from within the loaded swf. E.g. Reacting to stage resize.

    That’s me on all 3 counts, so I guess I have wasted the last 3 hours of my life. Sigh.

  73. February 21st, 2009 | 4:10 am

    Error 1120: Access of undefined property percent.
    percent.text = Math.ceil(perc*100).toString();

  74. March 9th, 2009 | 11:26 pm

    @Jeff dePascale

    How are you getting the actual loaded movie to be displayed? This is the method I call to handle prequel on completion:

    function loaderDone(e:Event):void
    {
    finishedLoad = true;
    var loadedObjects:Array = prequel.objects;
    this.addChild(loadedObjects[0]);
    }

  75. ventu
    March 22nd, 2009 | 4:07 am

    Hi, same problem as #57 Fraser:
    The swf seems to be playing while beeing loaded (and not beeind added to the display list yet) and starts somewhere not on frame one.

    How to prevent that inside the preloader?
    Thanks

  76. Pat
    March 30th, 2009 | 10:48 pm

    This solution works just fine in the flash environment, when testing with Simulate Download, however it does not work when deployed to a web server….there’s a long wait while the main movie loads and you only see the pre-loader for a split second just before the main clip shows….
    …at least that’s how it is for me.

    so the conclusion I have come to is that you DO have to worry about library linkage being set to load on first frame…..no???
    Pat

  77. saki
    May 8th, 2009 | 12:50 am

    Lee
    your tutorial had helped a great deal previously i used to do the two frame method but this thing had saved my neck & my sanity and Boosted my confidence in working in Flash

    God Bless you
    :::: ThankYou :::

  78. Alex
    May 19th, 2009 | 6:51 am

    Great stuff. Together with Kirks method in post 49 it helped me a lot, first by showing an easy and versatile way of preloading just about anything, and secondly (#49) by getting rid of any “null object” errors cased by trying to add stuff to a stage that is not there yet.

  79. renaud
    May 19th, 2009 | 6:09 pm

    3:08 in the morning. #49 made the thing. Thanks. Now I can sleep.

  80. daniel
    July 8th, 2009 | 11:05 pm

    thx lee, a great tut. makes a lot of sense.

    as for the repeated issues with error 1009 while trying to load file that access stage:
    add this in your loaded swf

    function onAddedToStage(evt:Event):void
    {
    // Access the stage properties here
    trace (stage.stageWidth);
    trace (stage.stageHeight);
    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.align = StageAlign.TOP_LEFT;
    stage.addEventListener(Event.RESIZE,resized);
    trace(“end of func”);
    }

    thx nebutch

    its 2:05 night night

  81. July 9th, 2009 | 11:37 pm

    Hi, Im just experimenting with as3 but I’m stuck on this preloader.
    I get it to work perfectly when I run it locally. It loads to a 100% and then loads the main swf. However when I upload this to the webserver the percentage jumps to ‘infinite’ instead. I dont understand why it should behave differently. Does anyone have a clue?

    var l:Loader = new Loader();
    l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
    l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
    l.load(new URLRequest(“index.swf”));
    var copy:String = ”;
    function loop(e:ProgressEvent):void
    {
    var perc:Number = e.bytesLoaded / e.bytesTotal;
    var loadProg = Math.ceil(perc*100).toString();
    loader.width = loadProg * 5;

    percent.text = loadProg+’%';
    etc…

  82. dilemma2k
    July 11th, 2009 | 5:30 pm

    plz reply lee
    i did all u said but i think my flash cs3 is set different to everyone
    else it says this to me when i press ctrl+Enter: Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

    PLZ HELP?

  83. mold
    July 12th, 2009 | 8:35 pm

    Hello Lee, great tutorial
    Im having the same issue as Fraser and ventu, while the external.swf is loading it also plays in the background, so I put a stop(); in the first frame of the external .swf. it would be nice if once the external file was loaded, the loader could do a gotoAndPlay into the loaded .swf

  84. July 12th, 2009 | 9:09 pm

    got it:

    var l:Loader = new Loader();
    l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
    l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
    l.load(new URLRequest(“external.swf”));

    function loop(e:ProgressEvent):void {
    var perc:Number=e.bytesLoaded/e.bytesTotal;
    percent.text=Math.ceil(perc*100).toString();
    }

    function done(event:Event):void {
    var loadedName:Object=event.currentTarget.content;
    loadedName.gotoAndPlay(‘begin’);
    removeChildAt(0);
    percent=null;
    addChild(l);
    }

  85. August 4th, 2009 | 10:07 am

    Mold

    I had the same problem as you and posts #57 and #75.
    Thanks for your solution. You’re a legend.

    I understand the fix but I don’t understand why the problem occurs in the first place.

    Does anyone have any ideas why the external movie would start playing early? (i.e before the done() method fires) ?

  86. September 2nd, 2009 | 8:11 am

    @Kirk – Thanks for that great fix! in #49.

    @Lee – great tutorial! as always

  87. October 14th, 2009 | 6:38 pm

    What is the best solution for this scenario:

    - loader.swf loads main.swf
    - main.swf also loads xml and images
    - you want to display the progress of main.swf xml and images into loader.swf progress bar.

  88. October 25th, 2009 | 4:00 pm

    Preloaders have always been an issue for AS3, especially with OOP. Thanks for sharing this, good stuff.

  89. Ashley
    November 25th, 2009 | 5:10 am

    Hi, I’m having a similar problem to sean(post 81),
    It all works perfect locally, then when it on my webserver,
    it loads to 100%, a stops, Doesn’t move on to show the swf,

    any help would be much appreciated,
    thanks

  90. Ashley
    November 25th, 2009 | 5:23 am

    I’m going to post the solution to my problem, just incase anyone has the same,

    My preloader.swf and content.swf are in the same folder, so I assumed when published in it’s html wrapper preloader.swf loading path would be from the same folder it’s in.

    However it actually take the path from where the swfObject is in the html,

    so instead of:
    l.load(new URLRequest(“content.swf”));

    it’s:
    l.load(new URLRequest(“assets/flash/content.swf”));

    hope this helps anyone who is as novice as i am,

  91. Mike
    January 15th, 2010 | 1:53 am

    Hi, does anyone know if it’s possible to make a external preloader that loads the main content outside of the preloaded .swf file and has the main content in a completely separate file?

    I’m trying to achieve this because I’m want my flash preloader to load in a different div tag than my main content.

    PLEASE any help would be appreciated. Thanks in advance.

    Mike

  92. January 22nd, 2010 | 4:18 am

    This tutorial really helped, a lot! I was wondering how I could use this code and preload all the swf files on my site? I am putting together a web design portfolio now, so i could really use this on other site I am doing as well.

    Thank you!

    Thomas Bryant

  93. Mick
    January 31st, 2010 | 10:47 am

    Add me to the list of people who think comment #49 kicks ass! I had a preloader that was showering me with 2068 and 1009 errors for days, but it works fine now. Thanks, Kirk!

  94. Nathan
    February 6th, 2010 | 10:24 pm

    I am trying this preloader and when I export as swf it works fine and loads the swf but when I upload it to a webserver on firefox it just shows “100″ and on IE it says Infinity and does not load in the external swf. Both the preloader and the external swf are on the webserver. I have tried seeing if it was due to case sensitivity or anything but nothing is working. HELP PLEASE

  95. Nathan
    February 6th, 2010 | 10:32 pm

    Thanks 90 for your fix! I will try that. nvm on my post 94

Leave a reply