Jan 23 2010

Unload an externally loaded swf file

Category: FAQadmin @ 12:09 am

How to remove or unload an externally loaded swf file from your project?

It happenes a lot that you try to load an external SWF file into your project. maybe in your portfolio or some swf movies or sound files. The problem I am going to talk about here is about the common problem of unloading the swf files!

you see, if you have loaded an swf file which has some music on it playing constantly, when you try to unload/remove that swf, the sound keeps playing! (not just sounds, maybe some Event.ENTER_FRAME or other listeners are still working)

to avoid this problem, the best solution I can think of is to add a REMOVED_FROM_STAGE listener in your external swf file so it can listen to when it’s removed from the stage so it can kill the running processes inside itself.

simply add it like this:

1
2
3
4
5
6
import flash.events.Event;
this.addEventListener(Event.REMOVED_FROM_STAGE, onStageRemoved);
function onStageRemoved(e:Event):void
{
// kill all listeneres and running processes here.
}

Regards,
Hadi

Tags: , , ,

3 Responses to “Unload an externally loaded swf file”

  1. Kriss says:

    Hi!

    Thanks for posting this tip! I struggled with this problem like so many others and non of the other solutions out there worked, but this one did.

    Thanks again and keep up the good work!
    Regards
    Kriss

  2. EMG says:

    Can you help me with this ?
    Here is my code for my current video player. I want to use this in a flip book. When the page is turned I need the video to stop playing.

    Below this code is my code for the video player. Where do I put the remove from stage code?

    import flash.events.Event;
    this.addEventListener(Event.REMOVED_FROM_STAGE, onStageRemoved);
    function onStageRemoved(e:Event):void
    {
    // kill all listeneres and running processes here.
    }

    My code:

    function videoOne(event:MouseEvent):void{
    pubPlayer.source=”bill.flv”;
    }
    function videoTwo(event:MouseEvent):void {
    pubPlayer.source=”jeff.flv”;
    }
    bill.addEventListener(MouseEvent.CLICK,videoOne);
    jeff.addEventListener(MouseEvent.CLICK,videoTwo);

Leave a Reply