Wednesday, July 16, 2008

Preloading in AS3 (Flash IDE) Example

I mentioned in a previous post that I was going to use the following source code as the base of all my future projects. So far it has worked quite well.

All you need to do to use this "DocumentBase" class is edit the first line within the init() function so that instead of loading "package.Class" you load whatever your document class would have been (Note: make sure you make a Symbol for your old document class in your library, even if the Symbol is empty and just targets class name). You will also need to place a marker called "READY" on the frame in the timeline that you would like everything to start on.

The benefit for using "getDefinitionByName" instead of using the class's normal constructor is that this will prevent Flash from loading your document class and all of its imports on Frame 1. Otherwise Flash wants to load everything the document class makes reference to. It doesn't matter if you set every library item to NOT load on Frame 1. It doesn't matter if you ensure the ActionScript publish settings export classes on Frame 2. It doesn't matter if you place all library items on Frame 3 and absolutely nothing is left on frame 1... Flash will still load all of your document class's imports on Frame 1. This is a big deal if you are using large libraries such as Papervision or even Tweener... it adds up.

I stumbled across the dynamic class loading while reading Moock's Charges Against AS3 earlier today. (Thanks for the link Bimal).

As always, let me know what you think. Would love to hear any ways to improve on it.


Anonymous said...

Hey Mark,

I've been using a simular class for a while now. I have had some problems with IE not triggering the "Event.COMPLETE" when the swf was cached, so I'm using an extra Event.Enterframe to compare loadedBytes and totalBytes. Have you ever experienced something like this?

Ickydime said...

I can't say that I have had any problem with IE not triggering the Event.COMPLETE. I'm using Flash 9.0.124 and IE 7.

However, I did have a hell of a time trying to get IE to give me anything but 0 on the ProgressEvent.bytesTotal while loading an external SWF. Works fine on the single swf, but loading a second seems to piss it off for some reason.

Shoot me an example if you would like. Will see if it triggers in my setup.

Unknown said...

So does basement_core.DocumentBase get entered as your Document Class or do you have to instantiate it in a frame?
Does your blank Symbol, that has a class path to the original Doc Class, need to be added to the stage or set to export in the first frame?

trinoo said...

Hello Mark,

I found this your old post as I'm fighting now with internal preloader in AS3. Your link to documentBase class file used in your post no longer works. Can you please make it available for download again? Thank you.