Roger Antonio
Roger Antonio

Reputation: 1

AS3 unloading SWF from loaded SWF itself

I've been messing with my AS3 codes trying to find a way to get a loaded swf unloaded by pressing te close_btn which is in the loaded SWF. I can't seem to get that to work, so Maybe you guys know a way to do this.

so:

main_swf loads swf_1.

And in swf_1 is the close_btn which should unload swf_1.

So that we get back to main_swf.

Any ideas?

Upvotes: 0

Views: 5192

Answers (2)

Jevgenij Dmitrijev
Jevgenij Dmitrijev

Reputation: 2238

main_swf has a loader which loads your swf_1.

my suggestion would be: If close_btn is on the main stage of the swf_1. When a close_btn gets clicked:

function handleClose ( e : MouseEvent ) : void
{
    // this needs to be in the root section of the swf_1
    this.dispatchEvent ( new Event ( Event.CLOSE ) );
}

inside the main_swf after the swf_1 has been loaded:

loader.content.addEventListener ( Event.CLOSE, handleContentClosed );

function handleContentClosed ( e : Event ) : void
{
    // removing the event listener, making the content ready for GC
    loader.content.removeEventListener( Event.CLOSE, handleContentClosed );
    // removing the actual content from the place it was added
    removeChild ( loader.content );
    // unloading and stoppping the loaded swf
    loader.unloadAndStop ();
}    

more info about unloadAndStop()

Upvotes: 0

www0z0k
www0z0k

Reputation: 4434

you can declare a following function in main_swf:

private function killSWF1():void{
    _loader.unloadAndStop();
}

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#unloadAndStop%28%29

and then pass it into the loaded swf:

private function onInit(e:Event):void{
    _mc = loader.content as MovieClip;
    _mc['toCall'] = killSWF1;
    addChild(_mc);
}

and in the loaded swf you'll have just to call it: toCall()

Upvotes: 0

Related Questions