我在一个项目中有一个要求,该项目必须从 javascript 调用 actionscript 3 函数。我从 stackoverflow 和 adobe 阅读了许多答案,并创建了一个简单的测试应用程序。在那里,我使用了 ExternalInterface.addCallback 来注册这两个函数,并且还在 actionscript 和 html 中包含了安全权限。
Action Script 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 is not a function"
在 Firefox 和类似的 Internet Explorer 中。
有什么遗漏吗?
最佳答案
在 Flash 中,您将clickhandle 公开为callas
ExternalInterface.addCallback("callas", clickhandle);
所以你应该从 javascript 调用 callas()
var hehe = document.getElementById('cvpanel');
hehe.callas();
关于javascript - 从javascript调用actionscript 3函数,addCallback不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40805668/