Unload Loaded Swf file by unloadAndStop()

by on 21/04/10 at 9:57 pm

hello friends ,

During my experiment I have noticed one important thing , that Loader.unload() method is not able to remove all the listeners attached to ‘stage’ by a loaded SWF nor it removes all the other instance like loaded swf’s sound , Timers  , movie clip animation etc.
In the following example i have simply used Loader.unload() method for removing the loaded swf.



So you have noticed in the above example that loaded swf’s sound is still playing even if you unload the loaded swf.Actually It is very difficult to remove a loaded SWF from memory after it is unloaded via Loader.unload() method.
But Flash Player 10 and CS4 made it very easy to unload the loaded swf very easily.Checkout the following example.

In ActionScript 3 there is a another  unloadAndStop() method. There is a big difference between unload() and unloadAndStop() method.By using unloadAndStop() all the unwanted listeners are removed and it seems that our SWF is garbage-collected.

Code :

unLoad_mc.addEventListener(MouseEvent.CLICK , unloadSwf);

unLoad_mc is my unLoad button instance and unloadSwf is the function where the loaded swf unloaded via unloadAndStop()method.

function unloadSwf(e:Event):void{
mLoader.unloadAndStop();
}

According to the ActionScript 3 current documentation, after calling Loader.unloadAndStop, the following happens:

1) Sounds are stopped
2) Stage event listeners are removed
3) Event listeners for enterFrame frameConstructed, exitFrame, activate and deactivate are removed
4) Timers are stopped
5) Camera and Microphone instances are detached
6) Movie clips are stopped.

I think this tips will help you out . So don’t forget to drop some comment here.

Thanks.

7 Responses to “Unload Loaded Swf file by unloadAndStop()”

  1. school grants

    May 7th, 2010

    Great information! I’ve been looking for something like this for a while now. Thanks!

  2. latisse online

    May 10th, 2010

    On the web there you go

  3. Andrew Li

    May 18th, 2010

    I’m having an issue with the unladAndStop();

    It works great in the above example but doesnt seem to work for me when I upload it to my server although it works in flash. Any idea how to get around this or is it a bug?

    • nikesh

      May 18th, 2010

      @Andrew .. one reason could be the version of flash but as you say this example works for you than this means you have the latest version of flash ….
      and if you used same code as above and having the latest version of flash than your file should work fine….

      Could you please share your code and the url where the file is located ? so that I can check the file and give you a better suggestion …

  4. mikaere

    May 11th, 2011

    Hi Nikesh, Great information. I’ve been looking for something like this too. I wonder if you can help me as I’m very new to action-script 3. I have created a flash site with 5 buttons (home,tvcs,film,contact and about). All buttons navigate to labels on the timeline. The tvcs and film buttons load external swfs which show flv videos. They both load up fine but unloading them seems to be my problem. They still remain onstage and play on while navigating through other pages. I’ve tried a few unload scripts but to no avail. I’m wondering where to add the unloadAndStop()method. into my current script in order for it to work properly. Much appreciated If you can help with my unloading dilemma. My code is as below:

    import flash.events.MouseEvent;

    home_btn.addEventListener(MouseEvent.CLICK,hClick);
    function hClick(event:MouseEvent):void{
    gotoAndStop(“home”);
    }

    tvcs_btn.addEventListener(MouseEvent.CLICK,tClick);
    function tClick(event:MouseEvent):void{
    gotoAndStop(“tvcs”);

    //load swf

    var _swfLoader:Loader;
    var _swfContent:MovieClip;
    loadSWF(“swfs/tvcs.swf”);

    function loadSWF(path:String):void {
    var _req:URLRequest = new URLRequest();
    _req.url = path;

    _swfLoader = new Loader();
    setupListeners(_swfLoader.contentLoaderInfo);

    _swfLoader.load(_req);

    }

    function setupListeners(dispatcher:IEventDispatcher):void {

    dispatcher.addEventListener(Event.COMPLETE, addSWF);
    dispatcher.addEventListener(ProgressEvent.PROGRESS,preloadSWF);
    }
    function preloadSWF(event:ProgressEvent):void {
    var _perc:int = (event.bytesLoaded / event.bytesTotal)
    * 100;
    // swfPreloader.percentTF.text = _perc + “%”;

    }
    function addSWF(event:Event):void {
    event.target.removeEventListener(Event.COMPLETE,addSWF);
    event.target.removeEventListener
    (ProgressEvent.PROGRESS, preloadSWF);

    _swfContent = event.target.content;
    _swfContent.addEventListener(“close”, unloadSWF);

    addChild(_swfContent);
    }
    function unloadSWF(event:Event):void {
    _swfLoader.unloadAndStop();

    removeChild(_swfContent);
    _swfContent = null;
    }
    }
    /////////////////////////
    film_btn.addEventListener(MouseEvent.CLICK,fClick);
    function fClick(event:MouseEvent):void {
    gotoAndStop(“film”);

    //load swf

    var _swfLoader:Loader;
    var _swfContent:MovieClip;
    loadSWF(“swfs/film.swf”);

    function loadSWF(path:String):void {
    var _req:URLRequest = new URLRequest();
    _req.url = path;

    _swfLoader = new Loader();
    setupListeners(_swfLoader.contentLoaderInfo);

    _swfLoader.load(_req);

    }

    function setupListeners(dispatcher:IEventDispatcher):void {

    dispatcher.addEventListener(Event.COMPLETE, addSWF);
    dispatcher.addEventListener(ProgressEvent.PROGRESS,preloadSWF);

    }
    function preloadSWF(event:ProgressEvent):void {
    var _perc:int = (event.bytesLoaded / event.bytesTotal)
    * 100;
    // swfPreloader.percentTF.text = _perc + “%”;

    }
    function addSWF(event:Event):void {
    event.target.removeEventListener(Event.COMPLETE,addSWF);
    event.target.removeEventListener
    (ProgressEvent.PROGRESS, preloadSWF);

    _swfContent = event.target.content;
    _swfContent.addEventListener(“close”, unloadSWF);

    addChild(_swfContent);
    }

    function unloadSWF(event:Event):void {
    _swfLoader.unloadAndStop();

    removeChild(_swfContent);
    _swfContent = null;
    }
    }
    //////////////////////////

    contact_btn.addEventListener(MouseEvent.CLICK,cClick);
    function cClick(event:MouseEvent):void{
    gotoAndStop(“contact”);
    }

    about_btn.addEventListener(MouseEvent.CLICK,aClick);
    function aClick(event:MouseEvent):void{
    gotoAndStop(“about”);
    }

    • nikesh

      May 19th, 2011

      @micaere … sorry for late reply ..

      you just need to check the _swfLoader and if exist … unload it every time whenever you clicking on other button .For that make a function where put a code to unload __swfLoader and call that function every time whenever user clicks on other button.

      hope this helps you …

  5. search

    Jul 16th, 2015

    Nice post. I was checking constantly this blog and I’m inspired!

    Extremely useful info specifically the remaining phase :) I care for such info much.
    I was seeking this particular information for a
    long time. Thanks and good luck.

Leave a Reply