Preloading in AS3 with Linked Items in your Library

In AS2 you could add all your library's linked items onto a frame that was never getting called to make sure they would preload correctly.  You can no longer do this in AS3.  In AS3 your linked items will still preload in frame 0 before your preloading code will be executed.  I found two work a rounds in ActionScript 3.  One way to do it is explained good at http://www.bit-101.com/blog/?p=946.  This way is a little tricky, especially for beginners.  You will have to use the [Frame] metadata tag.

The way I found that is easiest and fastest is to just have a blank swf that handles the preloading of your other swf.  This way you make sure your preloader will show up before frame 0.

Put the following code into the first frame of the swf that is handling the preloading.

stop();

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

function loop(e: progressEvent):void{ 
    var perc:Number = Math.round(e.bytesLoaded*100 / e.bytesTotal); 
    mcLoader.txtPercent.text = perc + "%";
}

function done(e:Event):void{ 
    l.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loop);
    l.contentLoaderInfo.removeEventListener(Event.COMPLETE, done); 
    removeChild(mcLoader); 
    addChild(l);
}
 
The only thing to be aware of is how to access you FlashVars and that the swf for the preloading is now your stage.


Source Code

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

Leave a comment

Submitted comments are subject to moderation before being displayed.

 Enter the above security code (required)

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.