PDA

View Full Version : Preloader for external swf??



kdayle815
August 6th, 2008, 03:03 PM
Hi All-

I'm a flash newbie, and I'd greatly appreciate any help!! I'm working on an image gallery with a set of nine buttons on the left side of my main .fla file, each linked to an external .swf. I was given some code that works wonderfully to accomplish this. However, since the external .swfs are rather large, I tried to add a preloader to each external .swf (the same one I used for my main .fla file). When I test the external swf by itself, the preloader works great! When I test it within the main movie the preloader for the external .swf's loops continuously, and the movie never plays (same thing happens when you publish the main movie).

So, my questions are:
1. Does anyone know what could be causing the preloader to loop??
2. OR, Is there a way to create a preloader that would work for all of the external swf's?


The following is the code I am using to make my buttons in the main timeline link to my external swf's:


//register the loadImage function to fire when the buttons are pressed
BTN1.addEventListener(MouseEvent.CLICK,loadImage);
BTN2.addEventListener(MouseEvent.CLICK,loadImage)
BTN3.addEventListener(MouseEvent.CLICK,loadImage)
//...add the other buttons here

function loadImage(e:MouseEvent){
//set a variable to hold the path of the images
var IMGPath:URLRequest;

//this sets the path of the image/swf depending on what button was pressed
switch(e.target){
case BTN1: IMGPath = new URLRequest("IMG1.jpg");
break;
case BTN2: IMGPath = new URLRequest("IMG2.jpg");
break;
case BTN3: IMGPath = new URLRequest("IMG3.jpg");
break;
default: trace("Error: loadImage called by unknown button");
break;
}

//create a loader and load the image
var IMGLoader:Loader = new Loader();
IMGLoader.load(IMGPath)

//you can position the swf wherever you want before you display it
IMGLoader.x = 166
IMGLoader.y = 46

//add the swf to the stage
addChild(IMGLoader)

}

kdayle815
August 7th, 2008, 11:39 AM
Ok so I had a suggestion that the reason my preloader for the external swf's is looping is because "the reference to your loaded swfs main timeline changes when it's loaded into another swf". The following is the code I was using for my preloader:


import flash.events.ProgressEvent;
function update(e:ProgressEvent):void
{
var percent:Number = Math.floor( (e.bytesLoaded*100)/e.bytesTotal );
if(preloaderMC is MovieClip){
preloaderMC.gotoAndStop(percent);
}
if(percent == 100){
play();
}
}
loaderInfo.addEventListener(ProgressEvent.PROGRESS , update);
stop();

So, as suggested I replaced my addEventListener line with new code that would account for the reference to the main timeline changing:


function update(e:ProgressEvent):void
{
var percent:Number = Math.floor( (e.bytesLoaded*100)/e.bytesTotal );
if(preloaderMC is MovieClip){
preloaderMC.gotoAndStop(percent);
}
if(percent == 100){
gotoAndPlay(2);
}
}
MovieClip(this.parent.parent).IMGloader.content.lo aderInfo.addEventListener(ProgressEvent.PROGRESS, MovieClip(this.parent.parent).IMGloader.content.up date);
stop();

But this still isn't working and I am getting the following error when I test the main swf: "TypeError: Error #1010: A term is undefined and has no properties."

Please please help! I am completely stuck with this problem :(

Felixz
August 8th, 2008, 12:30 PM
The fastest way is to use ProgressBar component
var loader:Loader=new Loader();
var progress:ProgressBar=new progress();
addChild(loader);
addChild(progress);
progress.source=loader.contentLoaderInfo;
loader.load(new URLRequest(url));

kdayle815
August 8th, 2008, 03:50 PM
Thnx for your reply Felix, I'm just now seeing it but I was able to come to a pretty good solution. :)