我在一个必须从javascript调用actionscript 3函数的项目中有一个要求。我从stackoverflow和adobe中读取了许多答案,并创建了一个简单的测试应用程序。在此,我使用了ExternalInterface.addCallback来注册这两个函数,并且还在动作脚本和html中都包含了安全权限。
动作脚本3嵌入式代码
我没有使用外部.as文件,而是使用操作面板来创建代码。
import fl.controls.Button
import flash.events.MouseEvent;
flash.system.Security.allowDomain("*")
var butt:Button = new Button();
addChild(butt);
butt.label = "hello";
butt.toggle = true;
butt.move(50, 50);
butt.height = 100;
butt.addEventListener(MouseEvent.CLICK, clickhandle);
function clickhandle(e:MouseEvent)
{
if(butt.height == 100)
butt.height = 200;
else butt.height = 100;
}
ExternalInterface.addCallback("callas", clickhandle);
测试应用将显示一个ac3按钮。单击该按钮后,它将通过单击处理程序“ clickhandle”来切换其高度的扩展和收缩。这是我打算从javascript调用的函数。
HTML代码
<html>
<head>
<script>
function callas() {
var hehe = document.getElementById('cvpanel');
console.log(hehe);
hehe.clickhandle();
}
</script>
</head>
<body>
<p>Test</p>
<object id='cvpanel' name='cvpanel' width="425" height="350" type="application/x-shockwave-flash" data="one.swf">
<param value="one.swf" name="movie">
<param name="allowscriptaccess" value="always"/>
<p>You need Flash to see it.</p>
</object>
</body>
</html>
该应用程序通过apache在http:// localhost下运行,并且所需的所有文件都在同一文件夹中。我可以运行该应用程序,并且可以看到按钮的出现。通过单击它们,它们将按预期切换高度。
当我从浏览器控制台调用函数callas时,控制台出现错误
“ TypeError:hehe.clickhandle不是函数”
在firefox中,在Internet Explorer中类似。
有什么遗漏吗?
在Flash中,您将clickhandle公开为callas
ExternalInterface.addCallback("callas", clickhandle);
所以你应该从javascript调用callas()
var hehe = document.getElementById('cvpanel');
hehe.callas();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句