Reputation: 11797
I am new to AS3, and would like to trigger an as3 function from javascript.
AS3
package code {
import flash.display.MovieClip;
import flash.external.ExternalInterface;
import flash.text.TextFormat;
public class Main extends MovieClip {
public function Main() {
ExternalInterface.addCallback("changesize", this.setStyle);
}
protected function setStyle() {
var tf:TextFormat = new TextFormat();
tf.size = 15;
editabletext.setTextFormat(tf);
}
}
}
html
<html><head>
<meta charset="UTF-8">
<title>animator</title>
<style type="text/css" media="screen">
html, body { height:100%; background-color: #ffffff;}
body { margin:0; padding:0; overflow:hidden; }
#flashContent { width:100%; height:100%; }
</style>
</head>
<body>
<div id="flashContent">
<object type="application/x-shockwave-flash" data="animator.swf" width="550" height="400" id="animator" style="float: none; vertical-align:middle">
<param name="movie" value="animator.swf">
<param name="quality" value="high">
<param name="bgcolor" value="#ffffff">
<param name="play" value="true">
<param name="loop" value="true">
<param name="wmode" value="window">
<param name="scale" value="showall">
<param name="menu" value="true">
<param name="devicefont" value="false">
<param name="salign" value="">
<param name="allowScriptAccess" value="always">
<a href="http://www.adobe.com/go/getflash">
<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player">
</a>
</object>
</div>
<script type="text/javascript">
var animator = document.getElementById('animator');
animator.changesize();
</script>
</body></html>
animator.changesize()
gives me Uncaught TypeError: animator.changesize is not a function
I have also tried changing allowscript access between sameDomain
and always
. neither seems to work
Upvotes: 0
Views: 645
Reputation: 52133
It's a timing issue. The callback will only get added to the DOM once the SWF has been loaded by the Flash Player and the Main
constructor is executed, but you are trying to call it immediately when the DOM is parsed.
Since the DOM is always going to be parsed before the SWF is loaded (and the callback is added) you should not assume that the SWF callback is always there and ready to be called. Instead, the SWF should call into JS when it is ready.
Upvotes: 1
Reputation: 51837
As mentioned in the comments, first check if ExternalInterface.available
is true and test on a server (could be localhost if you set one up locally).
I also notice you have an <object>
but no <embed>
tag. Some browsers use the object tag, while others use the embed tag. Make sure the id and name for these tags are the same.
Even better, I recommend using swfobject as it takes care of these issues and make the code a bit cleaner. Please have a look at the SWFObject External Interface article and example
Upvotes: 0