views:

394

answers:

1

I have an actionscript function that loads an external swf and is currently linked to a button in the same swf...

function btnClick(event:MouseEvent):void{
SoundMixer.stopAll();
 removeChild(loader); 
 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSWFLoaded);
 loader.load(movieSWF)
 loader.x=Xpos
 loader.y=Ypos
 addChild(loader)


}


button.addEventListener(MouseEvent.CLICK,btnClick);

I'm wondering if there is a way to call this function from a link on the page that the swf is housed on. I'm guessing javascript, php or swfaddress would be the most likely way, but I'm unbelievably new to all this so I'm not sure where to start or how to go about it.

Any help would be appreciated.

+3  A: 

You're looking for the ExternalInterface.addCallback function. This lets you register a function so that it can be called externally from JavaScript code.

Your HTML might look like this (where 'IDofSWF' is the ID of the element containing the SWF on the page):

<a href="#" onclick="document.getElementById('IDofSWF').clicky()">
    Click to call btnClick in the SWF
</a>

With the following in your initialization code in Flash:

import flash.external.ExternalInterface;

// ...

// This will allow JavaScript to call a function on the SWF named 'clicky'. It will execute the anonymous function passed as the second argument:
ExternalInterface.addCallback('clicky', function () {
    btnClick(null);
});

Note that you won't get a MouseEvent (my example passes null as the value for it) because the event handler is being called manually (externally from JavaScript) instead of from within the SWF on a mouse click.

Cameron