views:

401

answers:

2

I've read multiple examples on this, but I just don't get how it works.

  • How does the class know it's THE pre-loader?
  • How does flash know to load one class but not another?
  • How much can I do in a preloader? :-p

I'm using FlashDevelop atm and it's generating the project for me - however, from all the examples I checked they didn't explain how it worked either.

+1  A: 

In AS3 I only use external preloaders (they are a SWF) that then load in my main SWF. I ensure the preloader swf is small since you can't really preload the preloader. There is a class called Loader that you use to load in a SWF and get information about it (size etc). You can listen to events to receieve this information and then use it to render a progress bar etc.

So a preloader isn't anything that fancy just another class to do a job and so you tell it by passing in a URL String the swf you want to load.

Again a preloader is a class, you can do anything you want in it but best to keep the SWF file size down.

For a really great tutorial check out http://www.gotoandlearn.com/play?id=85. Lee also has a link to the files to download.

Allan
Yep, I understand how this method works - it's what FD/flash is doing in the example where it's a single swf that I can't seem to fathom :-p
widgisoft
ah ok from the other answer I take it that Flash Developer auto generates a preloader SWF or something? I use FDT so am not familiar with Flash Develop. Kinda cool though.
Allan
+2  A: 

Are you familiar with the Flash timeline?

By default, your SWF would have one frame. This frame contains your Preloader.as class. At compile-time, FD creates a second frame, which contains your Main class -- the class you want to be run once the whole SWF has downloaded. Preloader.as contains code that waits until all frames have been fully downloaded, and then instantiates the Main class.

More details from http://www.flashdevelop.org/community/viewtopic.php?f=9&t=5398:

When you create an "AS3 project with preloader", FD configures a few important things you should control:

  1. in Preloader.as, There is a call to resolve your main class: getDefinitionByName("{qualified main class name}")

  2. in Project properties, Compiler options > Additional compiler arguments [...] There should be: -frame start {qualified main class name}

Flash knows to run Preloader.as first because FD puts it in the first frame, and FD does this because the Preloader class has been set to "Always Compile" (you can choose to set this option yourself by right-clicking a class and selecting "Always Compile").

You can do whatever you like in a preloader, but it won't run any of its code until everything needed by the preloader has downloaded. So if you make a preloader with a 3MB image file and a progress bar, the progress bar won't do anything until the entire image has downloaded!

MichaelJW
Yep - Looks like "Always compile" is a bit misleading - You can only select a single file to be always compiled :-p
widgisoft
Coming from a purely procedural stand point and not having a real copy of flash I've heard of the time line but never used it :-p
widgisoft
Now that I realise FD is doing the magic behind the scenes I can investigate a little further; I love how as3 lets you control everything but I was just a little confused as to where this "magic" was coming from and whether or not it was configurable - mainly just to know how it works :-p
widgisoft