Christopher Lightfoot
Christopher Lightfoot

Reputation: 1167

How does the preloader work in as3?

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

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.

Upvotes: 1

Views: 2508

Answers (2)

MichaelJW
MichaelJW

Reputation: 136

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!

Upvotes: 2

Allan
Allan

Reputation: 3314

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.

Upvotes: 1

Related Questions