AS3和JS通信
运行环境:需要安装IIS,创建网站目录
AS3和外部容器通信方法比较简单:
import flash.text.TextField;
var txt:TextField = new TextField();
this.addChild(txt);
txt.text = "等待...";
//向js注册方法,JS调用as3Function这个方法,就是执行callback这个方法
ExternalInterface.addCallback("as3Function",callback);
function callback($param:String):void
{
txt.text = $param;
}
//直接调用SJ的方法getReady
ExternalInterface.call("getReady");
注意:先调用ExternalInterface.addCallback,再调用ExternalInterface.call
JS代码:
<html lang="en">
<head>
<meta Http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>FLASH与JS交互</title>
<script language="javascript">
var jsReady = false;
function pageInit()
{
jsReady = true;
}
function getReady()
{
if(jsReady)
{
//as3Function方法是flash注册的方法
getSwfInstance("FlashID").as3Function("成功");
}
else
{
return jsReady;
}
}
function getSwfInstance(name)
{
if (navigator.appName.indexOf("Microsoft") != -1)
{
return window[name];
}
else
{
return document[name];
}
}
</script>
</head>
<body onLoad="pageInit()">
<object id="flash" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400"
codebase="http://fpdownload.Macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0">
<param name="movie" value="test.swf">
<param name="quality" value="high">
<param name="bGColor" value="#F0F0F0">
<param name="menu" value="false">
<param name="wmode" value="opaque">
<param name="FlashVars" value="">
<param name="allowScriptAccess" value="sameDomain">
<embed id="embedID" src="test.swf"
name="FlashID"
width="550"
height="400"
align="middle"
quality="high"
bgcolor="#f0fff8"
menu="false"
play="true"
loop="false"
FlashVars=""
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/Go/getflashplayer">
</embed>
</object>
</body>
</html>
注意:swf的文件嵌入到html中的方法比较多,特别关键的要可以获取到所嵌入的swf对象,通过代码你可发现获取swf对象,是通过embed中的name属性。
相关文章