Skip to main content
Known Participant
April 22, 2010
Question

load in external swf on click - button

  • April 22, 2010
  • 1 reply
  • 8375 views

help please

was so easy in AS2 but seem really complicated in AS3, doh!

would someone please help?? I am trying to simply load in an external swf when I click a button.

I have created a button on the stage called it 'my_button'

I can get the swf to load in automatically but I am not understanding the script for loading it in on a click.....would someone pleae explain what script I need to have : (

this is the script I have for loading automatically;

var loader_mc : Loader = new Loader();
var urlRequest : URLRequest = new URLRequest("photography.swf");
loader_mc.load(urlRequest);
addChild(loader_mc);

works great just would like to have it load in on a button click, can someone help


aaaarrrgghhhh!!!!

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
April 22, 2010

:

yourbutton.addEventListener(MouseEvent.CLICK,f);

var loader_mc : Loader = new Loader();
var urlRequest : URLRequest = new URLRequest("photography.swf");

function f(e:Event){
loader_mc.load(urlRequest);
addChild(loader_mc);

}
DK1120Author
Known Participant
April 22, 2010

HA! HA! that works great, thank you!!

I have come accross one other thing when doing it, I am going to have 5 button in total each loading in external swfs for example

button 1 - loads in photography.swf

button 2 - loads in video.swf

button 3 - loads in art.swf

button 4 - loads in music.swf

button 5 - loads in print.swf


so I did this thinking I could just duplicate, create another button, call it 'my_video' and use . .

my_button.addEventListener(MouseEvent.CLICK, buttonClick)

function buttonClick(e:MouseEvent):void
{
     var loader_mc : Loader = new Loader();
      var urlRequest : URLRequest = new URLRequest("photography.swf");
      loader_mc.load(urlRequest);
      addChild(loader_mc);
}

my_video.addEventListener(MouseEvent.CLICK, buttonClick)
function buttonClick(e:MouseEvent):void
{
     var loader_mc : Loader = new Loader();
      var urlRequest : URLRequest = new URLRequest("video.swf");
      loader_mc.load(urlRequest);
      addChild(loader_mc);
}

and so ...

....but oh! know it just goes crazy,do I need to unload the swf that is already loaded also??

whats the simplest way of doing this

thanks for your help!

thanks for your help

kglad
Community Expert
Community Expert
April 22, 2010

you can't have 2 functions with the same name.  how will flash know which you want to use?

each button listener should call a different function or, you could call one function and use a property of your buttons to indicate which swf to load.

but the simplest is to use a different function for each button.

p.s.  please mark this thread as answered, if you can.