Skip to main content
Inspiring
April 28, 2011
Answered

Button to navigate and unload swf

  • April 28, 2011
  • 1 reply
  • 2135 views

I have the following code to load an external swf by clicking a button. It works fine.

btn_01_01.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);

function fl_ClickToLoadUnloadSWF(event:MouseEvent):void

{

var url:String = event.currentTarget.name+".swf";

fl_Loader.load(new URLRequest("loads/TEST_VID_AS3.swf"));

}

I also have a button that takes me to another place on the timeline.

this.btn_back_main.addEventListener(MouseEvent.CLICK,fback_main);

function fback_main(evt:MouseEvent)

{

MovieClip(this.root).gotoAndPlay("main");

}

But the loaded swf is still visible.

I would like that button to also unload the loaded swf.

Can anyone tell me if it's possible and suggest code?

Thank you in advance.

This topic has been closed for replies.
Correct answer Lee_Burrows

this should do it (assuming the btn to remove it is called "btn_remover"):

var ld:Loader = new Loader();

btn_01_02.addEventListener(MouseEvent.CLICK, v);

function v(e:MouseEvent):void {

     ld.load(new URLRequest("loads/TEST_VID_AS3.swf"))

     addChild(ld)

}

btn_remover.addEventListener(MouseEvent.CLICK, removeSwf);

function removeSwf(e:MouseEvent):void {

     ld.unloadAndStop();

}

firstly, i moved the Loader definition outside of the function (otherwise it gets removed at the end of the function and cant be accessed later)

then i added event listener to remove button to call removeSwf function - which calls unloadAndStop() on the loader object

fyi, i suggest you use more descriptive names for your buttons and functions otherwise you'll find it harder to work out whats going on if you come back to it after a break.

1 reply

leodsmithAuthor
Inspiring
April 28, 2011

I changed the load swf code to this:

btn_01_02.addEventListener(MouseEvent.CLICK,v);

function v(e:MouseEvent){

var ld:Loader = new Loader()

ld.load(new URLRequest("loads/TEST_VID_AS3.swf"))

addChild(ld)

}

Maybe I'll have better luck finding a way to unload.

:-)

Lee_Burrows
Participating Frequently
April 28, 2011

hi

you can use ld.unloadAndStop();

leodsmithAuthor
Inspiring
April 28, 2011

Thank you but how would I connect that line to the button? I really don't understand the language yet and am faking my way through this one while I learn.