AS2 → AS3: Main File Preloading
When I coded in AS2 I had a preloader that I liked to use over and over again for pretty much every file I did which always worked well for me and never had any issues. One thing to note, however, is the comments I made in the download files on Frame 2:
This frame holds all the symbols in your library that are going to be exported for ActionScript usage somewhere along the movie. I put them in frame 2 so that I can "use" them somewhere in the movie and not have "export in first frame" checked in the linkage for the symbols which allows me to not have to load them on frame 1 before the preloader script is run.
You will also notice if you go to File > Publish Settings > Flash and click on the Settings button that I set the export frame for classes to frame 2. This allows me to not export the classes before the preloader script as well and my preloader can then start at 0% rather than getting one of those nasty preloaders that don't show up until 75% of the movie has already loaded. Just make sure that the symbol you are using for your preloader graphic isn't some elaborate 50k image sequence or this will all be a moot point and your preloader will be too big to show up quickly.
Keep in mind that the Document class will be exported before the preloader no matter what so keep it to a minimum. Also, any classes that are used in the Document class will also be exported in frame 1 regardless of your settings for what frame to export classes in so also remember not to initiate too much heavy coding in the Document class.
That being said, here is the code I used in AS2:
-
stop();
-
-
var r:MovieClip = this;
-
-
// preloader
-
r.createEmptyMovieClip("preloader", r.getNextHighestDepth());
-
-
preloader.onEnterFrame = function():Void
-
{
-
var l:Number = r.getBytesLoaded();
-
var t:Number = r.getBytesTotal();
-
var mc:MovieClip = r.preloader_mc; // movie clip that has load animation
-
var total:Number = 100;
-
-
mc._xscale = Math.floor(total * (l / t));
-
percent_txt.text = Math.floor(total * (l / t)) + "%";
-
-
if (l == t && t> 200)
-
{
-
r.preloader.removeMovieClip(); // remove the preloader clip
-
-
// run these actions upon load
-
r.gotoAndStop("loaded");
-
}
-
};
And here is the new AS3 version of the same exact code:
-
stop();
-
-
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
-
-
function onEnterFrame($evt:Event):void
-
{
-
var l:Number = this.loaderInfo.bytesLoaded;
-
var t:Number = this.loaderInfo.bytesTotal;
-
var percent:Number = (l / t);
-
-
// code to handle visual part of preloader
-
this.preloader_mc.scaleX = percent;
-
this.percent_txt.text = Math.floor(percent * 100) + "%";
-
-
if (l == t && t> 0)
-
{
-
this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
-
-
// loaded, run these actions
-
this.gotoAndStop("loaded");
-
}
-
}
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.
6 Comments
You're absolutely right John, I updated the download and the code above.
Interesting approach to the linkage, i like it except for the fact that you have to make an extra MC to hold all the stuff.
I am putting a lot of code on the 3rd frame for my movie, and have all of my assets on the second frame that need to be exported for actionscript. Having a lot of code on the 3rd frame seems to prevent the preloader from showing until about 90% into it as when I remove the code and 'simulate download' it works fine. How can i fix this problem?
ive never seen anything like that before. normally the code is on the first frame or exported for actionscript on clips in the first frame, so i'm not exactly sure without seeing the FLA what the problem could be.
For anyone else who reads this, Jeff was using components in his file which resulted in the problems he outlined. Unfortunately I was unable to help Jeff because I never really use components in my projects but at least we found out that was the issue.
Same as your others example; your AS2 code doesn't match your AS3 one. For example you attach a movie you don't in AS3, or you add not needed code to AS2 like r = _this.
I'm starting to think you just want people to think AS3 is simpler, when it isn't.
Here the AS2 code that match your AS3 one:
stop();
function onEnterFrame($evt:Event):void
{
var l:Number = getBytesLoaded();
var t:Number = getBytesTotal();
var percent:Number = (l / t);
// code to handle visual part of preloader
preloader_mc._xscale = percent;
percent_txt.text = Math.floor(percent * 100) + "%";
if (l == t && t> 0)
{
delete onEnterFrame;
// loaded, run these actions
gotoAndStop("loaded");
}
}













I usually just put all 'linkage' marked items in a 'buffer' movieclip. Buffer has 2 frames, one empty with a stop and a 2nd frame where i just dump all the clips. Same idea, just different path i guess.
Though, on the loading note, i wouldn't just limit your loader check to (l == t), also add in a (l == t && t > 0) - i've actually had incidents where the total file size comes back wrong due to server settings. Mostly happens on external assets, but still worth catching.