如何从javascript调用flash actionscript回调方法?
我试图从JavaScript调用flash回调方法。 但它似乎不起作用。 flash动作脚本示例代码如下[简化]:如何从javascript调用flash actionscript回调方法?,javascript,jquery,flash,actionscript-3,swfobject,Javascript,Jquery,Flash,Actionscript 3,Swfobject,我试图从JavaScript调用flash回调方法。 但它似乎不起作用。 flash动作脚本示例代码如下[简化]: import flash.events.ActivityEvent; import flash.events.StatusEvent; import flash.external.ExternalInterface; var test_var = ExternalInterface.addCallback("js_method_to_call", flash_method);
import flash.events.ActivityEvent;
import flash.events.StatusEvent;
import flash.external.ExternalInterface;
var test_var = ExternalInterface.addCallback("js_method_to_call", flash_method);
function flash_method()
{
return "test";
}
function callFlashMethod(){
var flashFile = eval("window.document.test");
flashFile.js_method_to_call;
}
function loadTest(){
swfobject.embedSWF("test.swf", "test", "1", "1", "10.0.0", false);
}
$(document).ready(function(){
loadTest();
callFlashMethod();
});
javascript示例代码编写如下[简化]:
import flash.events.ActivityEvent;
import flash.events.StatusEvent;
import flash.external.ExternalInterface;
var test_var = ExternalInterface.addCallback("js_method_to_call", flash_method);
function flash_method()
{
return "test";
}
function callFlashMethod(){
var flashFile = eval("window.document.test");
flashFile.js_method_to_call;
}
function loadTest(){
swfobject.embedSWF("test.swf", "test", "1", "1", "10.0.0", false);
}
$(document).ready(function(){
loadTest();
callFlashMethod();
});
它总是在fire bug控制台中显示错误“flashFile.js_method_to_call is not a function”。我不确定这行:
var flashFile = eval("window.document.test");
我将使用:
var flashFile = document.getElementById("test");
另外,我猜这只是粘贴到这里时的一个输入错误,但是
flashFile.js_method_to_调用
应该是flashFile.js_method_to_call()代码>以下是一些应该非常有效的方法:
使用SWFObject.js嵌入Flash内容:
// Embedding through SWFObject rocks in comparison with Adobe shits:
var flashvars = {};
var params = {};
params.menu = "false";
params.salign = "t";
params.scale = "noscale";
params.wmode = "transparent";
params.allowScriptAccess = "always";
var attributes = {};
attributes.id = "${swf}";
swfobject.embedSWF("${swf}.swf", "flashDiv", "${width}", "${height}", "9.0.0", "", flashvars, params, attributes);
对HTML使用此选项:
<body>
<div id="flashDiv"></div>
</body>
调用Flash方法:
function callFlashMethod()
{
thisMovie("${swf}").js_method_to_call();
}
您获得了对嵌入SWF对象的引用,并使用它来调用as3方法
//AS3 Code
ExternalInterface.addCallback("helloFromJS",helloFromJS);
private function helloFromJS():void
{
trace("JS is saying hello");
}
//HTML Code
<object width="100%" height="100%" id="Test">
<param name="movie" value="Test.swf"/>
//JS Code
var swfObject = document.getElementById("Test");
swfObject.helloFromJS();
//AS3代码
addCallback(“helloFromJS”,helloFromJS);
私有函数helloFromJS():void
{
trace(“JS在打招呼”);
}
//HTML代码
//JS代码
var swfObject=document.getElementById(“测试”);
swfObject.helloFromJS();
这里有一个有趣且非常详细的教程
本页非常好地描述了解决方案,请尝试使该示例起作用。因此,你可以解决问题,弗拉基米尔·茨维特科夫的答案是完整的。我已经尝试了下面所有3个选项,但似乎不起作用document.getElementById(“test”)
,$(“#test”)
,eval(“window.document.test”)
我在firebug控制台thisMovie(“test”)中收到错误。js_方法调用不是函数
您是否使用我的示例中的js加载SWF?确保attributes.id=“test”
占位符变量${swf}
仅在您使用Flash Builder进行构建,并且此代码放置在index.template.html中,并且您已经对应用程序的入口点精灵进行了适当的注释时才有效。您是否尝试了window['test']而不是eval?我很确定这就是问题所在。