我在项目中有一个要求,它必须从javascript调用actionscript 3函数。我从stackoverflow和adobe中读了很多答案,并创建了一个简单的测试应用程序。在那里,我使用ExternalInterface.addCallback来注册这两个函数,并且我还在actionscript和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>
应用程序在http://localhost下通过apache运行,所需的所有文件都在同一个文件夹中。我可以运行应用程序,可以看到按钮出现。通过单击它们,他们可以按预期切换高度。
当我从浏览器控制台调用函数callas时,我在控制台中收到错误
“TypeError:hehe.clickhandle不是函数”
在firefox中和Internet Explorer中的类似内容。
有什么遗漏吗?
答案 0 :(得分:3)
在Flash中,您将 clickhandle 公开为 callas
ExternalInterface.addCallback("callas", clickhandle);
所以你应该从javascript调用 callas()
var hehe = document.getElementById('cvpanel');
hehe.callas();