ActionScript 3 External Interface callback is not working?
I would highly appreciate any help in getting this issue resolved. I have been at it for two days now and have not been able to resolve this i开发者_开发百科ssue. I am trying to call an AS3 function from JavaScript via ExternalInterface API which is simply not working at all. If I to call a JavaScript function form html loaded swf, it works beautifully.
I am following this tutorial: http://www.viget.com/inspire/bi-directional-actionscript-javascript-communication/
I am using swfobject to load the swf into a div.
JavaScript code:
<script type="text/javascript">
swfobject.embedSWF("testRun.swf", "myContent", "550", "400", "9.0.0");
function getFlashMovie(movieName)
{
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? window[movieName] : document[movieName];
}
function callFlash(flash)
{
//alert(flash);
getFlashMovie(flash).sendToFlash();
}
</script>
AS3 Code:
import flash.external.*;
import flash.events.*;
import flash.text.*;
JAVASCRIPT WILL CALL SENDTOFLASH WHICH THEN INVOKES THE INIT FUNCTION
ExternalInterface.addCallback("sendToFlash", init);
function init():void
{
var txt:TextField = txtField();
txt.appendText("LOOK AT ME!!!");
addChild(txt);
}
function txtField():TextField
{
var txt:TextField = new TextField();
txt.x = 25;
txt.y = 25;
txt.width = 200;
txt.height = 150;
txt.border = true;
txt.text = "INIT ";
return txt;
}
After hours and hours, I have managed to resolved the issue. There was nothing wrong with my code, the culprit was flash player security settings on my local machine. Instead of just adding the swf to my trusted sites with the flash play settings, I included the entire folder, and now it works like a charm.
This is how I changed the settings:
You can go about two ways of changing the settings: 1. when you open the html file, flash player global security box pops open 2. Open the html file, right click and select the 'Global Settings...' option from your context menu.
Once the setting panel is open, you will see four tabs:
Storage | Camer and Mic | Playback | Advanced
Go to 'Advanced' tab Click on "Trusted Local Settings..." Click on "Add..." Click on "Add Folder..." Locate the folder where you files such as the html and swf are. Click on "Confirm" Click on "Close"
And then close the Flash Player Global Settings panel
Now open your html file, if it is already opened then refresh your browser. It all should work now
精彩评论