PDA

View Full Version : Trouble calling AS3 function from JS


Blkhwks19
01-11-2012, 06:20 PM
Im attempting to call an AS3 function from JS, but its not working. Ive scoured the internet for days and Ive found lots of tutorials and instructions how to do this, but none of them have worked for me. So Im suspecting it might be something to do with my exact setup. Here's my code.

AS code:
import flash.external.ExternalInterface;

ExternalInterface.addCallback("testFunc", testFunc);

public function testFunc(msg:String):void
{
output.text = msg;
}


HTML/JS/SWFObject embed code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript" src="../scripts/swfobject.js"></script>
<script type="text/javascript">
function getFlashMovie(movieName)
{
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? window[movieName] : document[movieName];
}

var flash = getFlashMovie("flash");
flash.testFunc("hello from JS");

var flashvars = {
// custom flashvars here
};

var params = {
allowscriptaccess: "always",
scale: "noscale",
menu: "false"
};

var attributes = {
id: "mySWF",
name: "mySWF"
};

swfobject.embedSWF("../flash/mySWF.swf", "flash", "550", "400", "9.0.0", null, flashvars, params, attributes);
</script>
</head>
<body>

<div id="flash"></div>

</body>
</html>

Ive tried every method I can find and think of, and absolutely nothing. Can anyone please take a look at what I've got an let me know if I'm missing something or implementing something incorrectly? Thanks SO much in advance!!!

tadster
01-12-2012, 12:51 AM
The movie does not exist when you call getFlashMovie for it.
You need to place that code after the embedSWF call.

It's also best to let the flash load last, after the main js and html.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<body>

<div id="flash"></div>


<script type="text/javascript">
var flashvars = {
// custom flashvars here
};

var params = {
allowscriptaccess: "always",
scale: "noscale",
menu: "false"
};

var attributes = {
id: "mySWF",
name: "mySWF"
};

swfobject.embedSWF("../flash/mySWF.swf", "flash", "550", "400", "9.0.0", null, flashvars, params, attributes);

//you might even want to set a time out so that you can always be sure the swf is there first.

function getFlashMovie(movieName)
{
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? window[movieName] : document[movieName];
}

var flash = getFlashMovie("flash");
flash.testFunc("hello from JS");

</script>
</body>
</html>

Blkhwks19
01-12-2012, 03:21 PM
I tried your setup and it still doesnt work. I even tried adding a button to click to call the AS function. This way, The SWF could fully load and I could even wait another few seconds before clicking it and attempting to call the AS function. That also did not work.

Thanks for your advice, but Im afraid Im still at a stand-still with this....

tadster
01-12-2012, 05:21 PM
ok, next things to do is to just use document.getElementById

function getFlashMovie(movieName)
{
return document.getElementById(movieName);
}


And then in the AS3 you should wait for the stage to be available before setting the call back.

public function DocumentClass() {

addEventListener(Event.ADDED_TO_STAGE, waitForStage);

}
private function waitForStage(e:Event):void {

removeEventListener(Event.ADDED_TO_STAGE, waitForStage);

if(ExternalInterface.available) ExternalInterface.addCallback("testFunc", testFunc)
else throw new Error("External Interface not available.");

}

public function testFunc(msg:String):void
{
output.text = msg;
}

ASWC
01-12-2012, 05:50 PM
throwing an error if externalinterface is not available seems way disproportionate.

Blkhwks19
01-12-2012, 08:14 PM
That setup didnt work but Im getting a Javascript console error now. Heres what it says:

"Object doesn't support this proeprty or method"

And the line it is referring to is this line in the JS:

flash.testFunc("hello from JS");

So javascript isnt making the connection with AS (or vice versa), though Im not sure why. Im definitely registering the AS function (after being added to the stage) by calling ExternalInterface.addCallback("testFunc", testFunc) and that function definitely exists in my AS.

Im not sure whats preventing this from working....

ASWC
01-12-2012, 09:47 PM
ExternalInterface works just fine. If it doesn't for you it's because you are doing a mistake somewhere. If you are using Flash to compile this test then you shouldn't say that ExternalInterface is registering anything because it doesn't when compiling with Flash (no html context to work with at compile time). It's important to always give the right information if you expect the right help.

Blkhwks19
01-13-2012, 01:51 PM
I didnt mean ExternalInterface was literally registering anything, I simply meant the addCallback method was definitely being called properly. I was just confirming out loud that that's not whats causing my problem, is all.

Upon further testing I got it to work. I just started over this morning with the same setup and approach and it worked. Not sure if maybe something was being cached when it shouldnt (I typically disable the cache when I develop) but it works now so Im not going to complain. Thanks again tadster for the suggestions! =)