Skip to main content
miquael
Inspiring
April 27, 2010
Question

Preloading in AS 3.0

  • April 27, 2010
  • 3 replies
  • 702 views

Okay, how does one make a preloader in AS 3.0 for THE ENTIRE swf?

I'm certainly generally familiar with making preloaders in Flash, yet with changes to the Flash environment in the last few years, my situation may not be so straight forward:

I have a very developed Flash 3.0 application where the timeline is not used in the traditional way, everything is Class based, and there are all kinds of assets in the library that are used dynamically. 

This type of preloader is useless, because (I guess) that "this.loaderInfo" is associated to the timeline (which has nothing on it) :

     public function Preload () {

            this.loaderInfo.addEventListener(Event.COMPLETE, initApplication);

            this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, showProgress);

        }

        public function showProgress(theProgress:ProgressEvent):void {

            var percent:Number = Math.round((theProgress.bytesLoaded / theProgress.bytesTotal )*100 );

            trace(percent + " %");

            this.graphics.clear()

            this.graphics.lineStyle(1, 0xFF0000, 1)

            this.graphics.beginFill(0xFF0000)

            this.graphics.drawRect(100,100,percent,10)

        }

        public function initApplication(myEvent:Event):void {

            trace("Loaded !");

            init();

        }

After I found some reference to a tip on-line, I went thru my library and adjusted the Linkage properties (for the movie clips that are Linked to Classes) to NOT be "export in frame 1", yet I'm not really certain what this does, and doing this started to create other problems.  Is that any kind of solution in this case?

Isn't there a straight forward way to go about this?  A property of the SWF that includes ALL of the ASSETS of the SWF independent of the timeline?

This topic has been closed for replies.

3 replies

Participant
April 28, 2010

It is possible to add a preloader inside a single swf, without adding another swf. The preloader that comes by default in a Flex application is evidence for that.

You basically only need to add another frame to your swf (it must have at least two frames).

The first frame contains only the assets relevant for showing the pre-loader - no reference to the main class.

You then have to instantiate your main class by using getDefinitionByName().

Her is a detailed tutorial:

http://www.bit-101.com/blog/?p=946

Participating Frequently
April 27, 2010

Get youself a good book or tutorial about OOP and AS 3.0 its a good start.

miquael
miquaelAuthor
Inspiring
April 27, 2010

Hi.  Thanks for the advice.  I already have a few good books on OOP and AS 3.0, and have been programming AS 3.0 OOP since 2007--yet this very specific problem eludes me and is not mentioned in the books I have.

So seeking assistance from anyone who may have already have experience with implementing this type of preloader in AS 3.0.

April 27, 2010

What I would suggest, and this is how I always do it when I need a preloader, is to just make yourself a new movie and load into it. Then just a little timeline code is all you need:

var l:Loader = new Loader();

l.addEventListener(Event.COMPLETE, swfLoaded, false, 0, true);

l.addEventListener(ProgressEvent.PROGRESS, prog, false, 0, true);

l.load(new URLRequest("myMovie.swf"));

function prog(e:ProgressEvent):void

{

     //update percent bar

}

function swfLoaded(e:Event):void

{

     addChild(l);

     //start loaded swf

}

You can have an init method in the swf to be loaded that is called from inside swfLoaded() when you add it to the display list if you need.

miquael
miquaelAuthor
Inspiring
April 27, 2010

i mean, Flex (Flash Builder) does this pretty seamlessly, right?  I hope I don't have to transfer the entire project to Flex just to do this ...