The complied SWF is not showing the preloader until the whole SWF completely loaded. Any hepl would be really appreciated, I googled the whole night couldnt find anything on this, at least for me.
2 Answers
You can not embed the ByteArray
into your main document class, because classes that are referenced from the document class will automatically be included on frame 1.
The best way to preload assets is to have a separate Preloader class and Main class. You want your Preloader class to export on frame 1, and your Main class and assets on frame 2.
Unfortunately, this is trickier than it should be, but here's how you can do it: Set your document class to Preloader
. This class contains your loaderInfo
code. However, when you finish loading, do not instantiate Main directly, i.e. do not do var main:Main = new Main()
. This will automatically cause Main to be compiled onto frame 1 regardless of what you do. Instead, instantiate it indirectly, like this:
nextFrame(); // you sometimes need to do this for the player to register classes exported on frame 2
var mainClass:Class = flash.utils.getDefinitionByName("Main") as Class;
var main:Sprite = new mainClass();
addChild(main);
This will stop the compiler from automatically yanking Main onto frame 1.
Next, if you are using the Flash CS3+ IDE, go to File->Publish Settings->Flash->ActionScript 3.0 settings and change the "Export classes on frame" setting to frame 2. Then, on frame 2 of your movie, place an empty MovieClip. Inside this MovieClip, place a reference to your Main class by putting this code: var dummy:Main;
. The reason you have to do this is so that the compiler will still know you are using Main, so it will actually compile it into the movie, otherwise it would not compile it at all. You also don't want to put this on the main timeline, because any code references on the main timeline automatically get yanked onto frame 1.
In the IDE, a helpful trick to check that things have exported in their proper place is to check "Generate size report" in Publish Properties->Flash. You can examine the report and will easily notice if junk has exported onto frame 1.
If you are using Flash Builder, FlashDevelop, or FDT, the process is basically the same--create separate Preloader and Main classes, and instantiate Main indirectly from Preloader. But to signal the compiler to compile Main on a frame after Preloader, put this metatag above public class Main
in Main.as
:
[Frame(factoryClass="Preloader")]
FlashDevelop can also introspect SWF files by click on the + beside it in the Project tab. It will show you what assets have been exported onto which frames. Ideally, you only want the bare minimum of Preloader on frame 1.

- 618
- 2
- 9
- 24

- 1,549
- 1
- 9
- 14
-
wow! great trick but then i get this error message - ArgumentError: Error #2100: The ByteArray parameter in Loader.loadBytes() must have length greater than 0. – kornesh Mar 27 '11 at 21:21
-
Not sure on that one, could you post the code you are using to create the ByteArray? – Mike Welsh Mar 27 '11 at 21:45
-
Flash complier didnt compile the BinaryArray in the swf, the swf size is just few KBs instead of MBs – kornesh Mar 27 '11 at 22:05
-
@kornesh: Edit your **question** to include your code, not the **answer**. – BoltClock Mar 27 '11 at 22:15
-
Without knowing the particulars of how the `AESKey` works, I would check the following: 1) In your shell, check that the file `compressed` actually compressed correctly and contains some data. 2) `trace(data.length);` before and after the decompression in `init()`. Verify that it's not 0. 3) `[Embed]` the normal, uncompressed SWF, skip the decompression steps, and check if that loads correctly. 4) Generate a size report and verify that `compressed` was exported (look for `foo_fla.Main_content`) – Mike Welsh Mar 27 '11 at 22:54
-
the `compressed` file in not embed in the final SWF, I dont know why and how to solve it – kornesh Mar 27 '11 at 23:10
I've tested Mike's method in CS4. Apparently the dummy movie clip that references Main doesn't need to be on frame 2, or anywhere on the timeline for that matter. As long as the dummy movie clip is set to export on frame 2, Main will export on frame 2 because its referenced in the dummy clip's code.

- 11,616
- 6
- 39
- 66