如何从javascript调用flash actionscript回调方法?

我试图从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); function flash_method() { return "test"; } 

javascript示例代码写在[简化]下面:

  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(); }); 

它总是在火灾控制台“flashFile.js_method_to_call不是函数”中显示错误。

这里的东西应该非常好用:

  1. 使用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); 
  2. 将此用于HTML:

      
  3. 要调用Flash方法,请使用以下模式:

     // Functions needed for calling Flex ExternalInterface function thisMovie(movieName) { if (navigator.appName.indexOf("Microsoft") != -1) { return window[movieName]; } else { return document[movieName]; } } 
  4. 调用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   //JS Code var swfObject = document.getElementById("Test"); swfObject.helloFromJS(); 

这里有一个有趣且非常详细的教程http://bytes.com/topic/flash/answers/694359-how-do-i-access-flash-function-using-javascript#post2759970

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6

此页面非常好地描述了解决方案,只是尝试使该示例工作。 所以你可以解决问题,弗拉基米尔Tsvetkov的答案是完整的。

我不确定这一行:

 var flashFile = eval("window.document.test"); 

我会用:

 var flashFile = document.getElementById("test"); 

另外,我猜这只是在这里粘贴时的错误,但是flashFile.js_method_to_call; 应该是flashFile.js_method_to_call();

    Interesting Posts