PDA

View Full Version : Preloading pure AS3 SWFs -- do they play before loading?



krazykat
October 22nd, 2008, 02:22 PM
Here's something the I and the other AS developer here never really grokked, although it seems like it should be super basic. :h:

I've got a large (2 month) project written entirely in AS3 (i.e. no IDE). Someone else has written a nice basic preloader that will load my SWF into the page.

The only problem is that it isn't working for my SWF. I'm not getting the COMPLETE event to signal loading done -- and my SWF is starting to run before it's all loaded. Now, I assume that this is the normal behavior that I've just forgotten. But how, then, should one load an external SWF if one wants enough to load so that the user can interact, but not so much that the loaded SWF is already starting to do its thing w/o the user?

The preloader loads in a simple animation and then unloads it on COMPLETE.



var l:Loader = new Loader();
var ploader:PreLoader = new PreLoader();

addChild(ploader);
ploader.x = 410;
ploader.y = 140;
trace(ploader.x , ploader.y);
ploader.debug.text = "loading";


l.contentLoaderInfo.addEventListener(ProgressEvent .PROGRESS, loop, false, 0,true);
l.contentLoaderInfo.addEventListener(Event.COMPLET E, done, false, 0,true);
l.contentLoaderInfo.addEventListener(IOErrorEvent. IO_ERROR, error, false, 0,true);
l.load(new URLRequest("main.swf"));

function loop(e:ProgressEvent):void
{
var perc:Number = Math.ceil((e.bytesLoaded / e.bytesTotal) * 100);
ploader.loadProgress_txt.text = perc.toString();
this.ploader.preLoaderBurst_mc.maskComp_mc.gotoAnd Stop(perc);
trace("perc = " + perc);
}

function done(e:Event):void
{
ploader.debug.text = e.toString();

removeChild(ploader);
ploader.loadProgress_txt = null;
addChild(l);
}

function error($error:IOErrorEvent):void {
ploader.debug.text = $error.toString();
}

wvxvw
October 22nd, 2008, 03:48 PM
How do you know it is playing if you add the loader to the display list after it is loaded? You either shouldn't see it at all or you do to it something else in some other place in the code.

BTW. SWF may be downloaded progressively, meaning, each frame will start playing after it is completely loaded, but, as I understand, you have only 1 frame, so, you shouldn't see anything while the movie loads...

krazykat
October 22nd, 2008, 03:55 PM
Because I can see the server hits in the browser status bar consistent with loading the multiple FLVs my SWF is loading.

But the answer seems to be this: to put my main init code in the loaded SWF in a separate function and not to call that function until the loaded SWF gets an added-to-stage event. This seems to work fine. I'm unclear as to the reason behind this though -- Perhaps Senocular or some in-the-know person could answer?

As an aside, I've notice that in TextMate's AS3 bundle (the one I'm using in any case) the Sprite subclass template is set up to call an init function on a Stage.resize event -- is this meant to work the same as above? Or another rationale?


How do you know it is playing if you add the loader to the display list after it is loaded? You either shouldn't see it at all or you do to it something else in some other place in the code.

BTW. SWF may be downloaded progressively, meaning, each frame will start playing after it is completely loaded, but, as I understand, you have only 1 frame, so, you shouldn't see anything while the movie loads...