Calling AS3 function from html link - Javascript? php? swfaddress?

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.

Answers


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.


Need Your Help

VS 2015, C# 6, MVC5, Roslyn -- 502 gateway errors on Azure web app

asp.net-mvc azure visual-studio-2015 azure-web-sites roslyn

I'm in the process of diagnosing this issue and I will update as I learn more but I wanted to post what I have in case there's a quick fix/someone's seen this already.

connecting to RabbitMQ server with external ip

rabbitmq mqtt paho

I am trying to implement MQTT protocol using RabbitMQ. I have installed RabbitMQ server and changed the MQTT port from 1883 to 80.I am able to publish and subscribe to the server from the network. ...