1

Got a question for you all! I'm trying to make a navigation system for my website where each nav item is loaded as an external .swf. Thanks to another user on this site, I was able to get that functionality in place. The problem is I want to add a preloader for these external swfs as well.

The code I'm working with is this:

var loadedSWF:Loader = null;

/**
 * Loads an SWF and adds it to container once complete
 * @param file The URL to the SWF to load
 * @param container The container to add the SWF to
 */
function loadSWF(file:String, container:MovieClip=null):void
{   
    if(container == null) container = MovieClip(root);

    // removes the previously loaded SWF
    if(loadedSWF != null)
    {
        if(loadedSWF.parent) loadedSWF.parent.removeChild(loadedSWF);
    }

    var req:URLRequest = new URLRequest(file);
    loadedSWF = new Loader();
    loadedSWF.load(req);

    addChild(loadedSWF);
}



menu_mc.test_btn.addEventListener(MouseEvent.CLICK, _click);
function _click(e:MouseEvent):void
{
    loadSWF("testmovie.swf");
    loadedSWF.x = 0;
    loadedSWF.y = 125;
    var otherindex = getChildIndex(Border);
    setChildIndex(loadedSWF, otherindex + 1);
}

So far everything I've tried has come up short. I can get a preloader working if I don't use a null loader, but when I do that I'm not sure how to get it to remove the assets being used when loading another .swf - everything just stacks and bogs down the site. If I try and put together a preloader attached to the above code, I get errors because I'm making calls to a null object. Sorry to be such a newbie, I've only just begun to wrap my head around flash. I appreciate any help!

Edit: Here's a quick look at my somewhat jumbled version that includes a preloader, but doesn't work due to the null loader:

var loadedSWF:Loader = null;

/**
 * Loads an SWF and adds it to container once complete
 * @param file The URL to the SWF to load
 * @param container The container to add the SWF to
 */
function loadSWF(file:String, container:MovieClip=null):void
{   
    if(container == null) container = MovieClip(root);

    // removes the previously loaded SWF
    if(loadedSWF != null)
    {
        if(loadedSWF.parent) loadedSWF.parent.removeChild(loadedSWF);
    }

    loadedSWF.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
    loadedSWF.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
    var req:URLRequest = new URLRequest(file);
    loadedSWF = new Loader();
    loadedSWF.load(req);

    function loadProdComplete(e:Event):void {
    trace("swf file loaded");   
    //remove the preloader from container clip
            removeChild(preLoader);

           // add the loaded swf to container clip
    addChild(loadedSWF);    

    currentSWF = MovieClip(loadedSWF.content);
    currentSWF.gotoAndPlay(1);

    currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);

function checkLastFrame(e:Event):void { 

if (currentSWF.currentFrame == currentSWF.totalFrames) {
     currentSWF.stop();
    // trace("stopped");     
   }

   }   

}

var preLoader:loader = new loader();

//position the loading bar
preLoader.x = 155;
preLoader.y = 185;

addChild(preLoader);

function onProgressHandler(event:ProgressEvent){

var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";

trace(preLoader.bar.scaleX );

   }

}

var currentSWF:MovieClip = new MovieClip();


menu_mc.test_btn.addEventListener(MouseEvent.CLICK, _click);
function _click(e:MouseEvent):void
{
    loadSWF("testmovie.swf");
    loadedSWF.x = 0;
    loadedSWF.y = 125;
    var otherindex = getChildIndex(Border);
    setChildIndex(loadedSWF, otherindex + 1);
}

Edit #2: Functional code, but now receiving the following output error whenever I hit the button. The swfs load and unload just fine, but if anyone knows how I could clean it up to avoid the output error I'd like to get it as smooth as possible!:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at MethodInfo-8()

var container:MovieClip = new MovieClip();
var currentSWF:MovieClip = new MovieClip();
var swfLoader:Loader = new Loader();

function launchSWF(vBox, vFile):void{   

//vBox.addChild(swfLoader);
var swfURL:URLRequest = new URLRequest(vFile);

swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);

swfLoader.load(swfURL);

function loadProdComplete(e:Event):void {
    trace("swf file loaded");   
    vBox.removeChild(preLoader);
    vBox.addChild(swfLoader);   

    currentSWF = MovieClip(swfLoader.content);
    currentSWF.gotoAndPlay(1);


    currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);

function checkLastFrame(e:Event):void { 

    if (currentSWF.currentFrame == currentSWF.totalFrames) {
     currentSWF.stop();
    // trace("DONE");     
   }

   }   

}

var preLoader:loader = new loader();
preLoader.x = 155;
preLoader.y = 185;

vBox.addChild(preLoader);

function onProgressHandler(event:ProgressEvent){

var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";

trace(preLoader.bar.scaleX );

   }    

}

test_btn.addEventListener(MouseEvent.CLICK, _load); 

function _load(e:Event):void{

     swfLoader.unloadAndStop();
     var swfFile:String = 'test.swf';
     launchSWF(container, swfFile);
     //put it on stage
     addChild(container);
}
Community
  • 1
  • 1
DoomJTHM
  • 13
  • 5

1 Answers1

0

Dont use null , just use the unload method of the Loader class.

Loader.unload()
mpm
  • 20,148
  • 7
  • 50
  • 55
  • Thanks, I'm trying to get this to work (documentation seems to be pretty poor, so it's easier for me to learn by picking apart the functional aspects of existing code) but so far attempts at inserting either unload or unloadAndStop have yielded Error 1061: Call to a possibly undefined method unloadAndStop through a reference with static type Class, so I can't even begin to sort out precisely how I'll end up wrapping the rest of the script around it. The only information I could find about this error was that I needed to run in Flash 10 or higher, but I'm already doing that. =| – DoomJTHM Mar 24 '12 at 15:25
  • hi , you need to call unload on an instance of the loader of course , so : loader:Loader = new Loader(); then later in the script loader.unload(). – mpm Mar 25 '12 at 01:37
  • Great, thanks again! I managed to get a functional code working, but now I'm receiving an output error. I've edited the original post with the code I'm using and the error I'm getting. – DoomJTHM Mar 25 '12 at 16:23