1
votes

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);
}
1

1 Answers

0
votes

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

Loader.unload()