Skip to main content
Known Participant
November 29, 2010
Question

Buttons and external swfs

  • November 29, 2010
  • 1 reply
  • 2332 views

I would like to automatically load an external swf inside an existing empty mc (or a new one)  and load/unload different external swf on several button click fuctions.  I've read the other posts, but none that quite facilitates accomplishing this.

This is what I have, I know it's messy- but I've been trying so many different methods until its probably loaded with irrelevant functions!

var myLoader:Loader = new Loader(); addChild(myLoader); var url:URLRequest = new URLRequest("homepage.swf"); myLoader.load(url);
  myLoader.x=477;
  myLoader.y=0;


/* Click to Load/Unload SWF or Image from a URL.
Clicking on the symbol instance loads and displays the specified SWF or image URL. Clicking on the symbol instance a second time unloads the SWF or image.

Instructions:
1. Replace "http://www.helpexamples.com/flash/images/image1.jpg" below with the desired URL address of the SWF or image. Keep the quotation marks ("").
2. Files from internet domains separate from the domain where the calling SWF resides cannot be loaded without special configuration.
*/

button_1.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);

var fl_Loader:Loader;

//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad:Boolean = true;

function fl_ClickToLoadUnloadSWF(event:MouseEvent):void
{
if(fl_ToLoad)
{
  fl_Loader = new Loader();
  fl_Loader.load(new URLRequest("homepage.swf"));
  fl_Loader.x=477;
  fl_Loader.y=0;

  removeChild(myLoader);
  removeChild(fl_Loader_2);
  removeChild(fl_Loader_3);
  removeChild(fl_Loader_4
  addChild(fl_Loader);
);
 
}
else
{
  fl_Loader.unload();
  removeChild(fl_Loader);
  removeChild(myLoader);
  removeChild(fl_Loader_2);
  removeChild(fl_Loader_3);
  removeChild(fl_Loader_4);
  fl_Loader = null;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad = !fl_ToLoad;
}


/* Click to Load/Unload SWF or Image from a URL.
Clicking on the symbol instance loads and displays the specified SWF or image URL. Clicking on the symbol instance a second time unloads the SWF or image.

Instructions:
1. Replace "http://www.helpexamples.com/flash/images/image1.jpg" below with the desired URL address of the SWF or image. Keep the quotation marks ("").
2. Files from internet domains separate from the domain where the calling SWF resides cannot be loaded without special configuration.
*/

movieClip_2.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_2);

var fl_Loader_2:Loader;

//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad_2:Boolean = true;

function fl_ClickToLoadUnloadSWF_2(event:MouseEvent):void
{
if(fl_ToLoad_2)
{
  fl_Loader_2 = new Loader();
  fl_Loader_2.load(new URLRequest("portfolio.swf"));
  fl_Loader_2.x=477;
  fl_Loader_2.y=0;

  removeChild(myLoader);
  removeChild(fl_Loader);
  removeChild(fl_Loader_3);
  removeChild(fl_Loader_4);
  addChild(fl_Loader_2);

}
else
{
  fl_Loader_2.unload();
  removeChild(fl_Loader_2);
  removeChild(fl_Loader);
  removeChild(fl_Loader_3);
  removeChild(fl_Loader_4);
  fl_Loader_2 = null;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad_2 = !fl_ToLoad_2;
}

/* Click to Load/Unload SWF or Image from a URL.
Clicking on the symbol instance loads and displays the specified SWF or image URL. Clicking on the symbol instance a second time unloads the SWF or image.

Instructions:
1. Replace "http://www.helpexamples.com/flash/images/image1.jpg" below with the desired URL address of the SWF or image. Keep the quotation marks ("").
2. Files from internet domains separate from the domain where the calling SWF resides cannot be loaded without special configuration.
*/

movieClip_3.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_3);

var fl_Loader_3:Loader;

//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad_3:Boolean = true;

function fl_ClickToLoadUnloadSWF_3(event:MouseEvent):void
{
if(fl_ToLoad_3)
{
  fl_Loader_3 = new Loader();
  fl_Loader_3.load(new URLRequest("about.swf"));
  fl_Loader_3.x=477;
  fl_Loader_3.y=0;

  removeChild(myLoader);
  removeChild(fl_Loader_2);
  removeChild(fl_Loader);
  removeChild(fl_Loader_4);
  addChild(fl_Loader_3);

}
else
{
  fl_Loader_3.unload();
  removeChild(fl_Loader_3);
  removeChild(fl_Loader_2);
  removeChild(fl_Loader);
  removeChild(fl_Loader_4);
  fl_Loader_3 = null;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad_3 = !fl_ToLoad_3;
}

/* Click to Load/Unload SWF or Image from a URL.
Clicking on the symbol instance loads and displays the specified SWF or image URL. Clicking on the symbol instance a second time unloads the SWF or image.

Instructions:
1. Replace "http://www.helpexamples.com/flash/images/image1.jpg" below with the desired URL address of the SWF or image. Keep the quotation marks ("").
2. Files from internet domains separate from the domain where the calling SWF resides cannot be loaded without special configuration.
*/

movieClip_4.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_4);

var fl_Loader_4:Loader;

//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad_4:Boolean = true;

function fl_ClickToLoadUnloadSWF_4(event:MouseEvent):void
{
if(fl_ToLoad_4)
{
  fl_Loader_4 = new Loader();
  fl_Loader_4.load(new URLRequest("contact.swf"));
  fl_Loader_4.x=477;
  fl_Loader_4.y=0;

 
  removeChild(myLoader);
  removeChild(fl_Loader_2);
  removeChild(fl_Loader_3);
  removeChild(fl_Loader);
  addChild(fl_Loader_4);

}
else
{
  fl_Loader_4.unload();
  removeChild(fl_Loader_4);
  removeChild(fl_Loader_2);
  removeChild(fl_Loader_3);
  removeChild(fl_Loader);
  fl_Loader_4 = null;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad_4 = !fl_ToLoad_4;
}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
November 29, 2010

1.  you can't load into a movieclip.

2.  you load external swfs using a loader

3.  you can add a loader to a movieclip to make the loader a child of the movieclip.

4.  if you only want to load, at most, one external swf at any one time, you can use one loader for all your external swf loads.

so, if you want to load "one.swf" when you app starts, you can use:

/////////////////////////////////////////////////////

var loader:Loader=new Loader();

loader.load(new URLRequest("one.swf"));

addChild(loader);

///////////////////////////////////////////////

if you also want to use that loader to load another swf when a button is clicked, you can use:

yourbutton.addEventListener(MouseEvent.CLICK,f);

function f(e:Event):void{

load.load(new URLRequest("another.swf"));

}

Known Participant
November 29, 2010

kglad, it's removing the one.swf and playing the another.swf file when the button is clicked- but i can still hear the video from the one.swf. Apparently, it's hiding it, but not unloading it.

kglad
Community Expert
Community Expert
November 29, 2010

if you have video you must either:

1.  explicitly stop the video

2.  use the unloadAndStop() method applied to your loader (if you're publishing for fp 10)

3.  use SoundMixer.stopAll();