如何从 JavaScript 调用 Flash ActionScript 回调方法?

发布于 2024-12-08 09:34:56 字数 741 浏览 1 评论 0原文

我尝试从 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();
 });

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

I tried to call a flash callback method from JavaScript.
But it seems not working.
The flash action script example code is like below [Simplified]:

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"; 
}

The javascript example code is written below [Simplified]:

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

It is always display the error in fire bug console "flashFile.js_method_to_call is not a function".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

昔梦 2024-12-15 09:34:56

下面的内容应该非常有效:

  1. 使用 SWFObject.js 嵌入 Flash 内容:

    // 通过 SWFObject 嵌入与 Adob​​e 的垃圾相比简直是太棒了:
    var flashvars = {};
    
    var 参数 = {};
    params.menu = "假";
    params.salign = "t";
    params.scale = "noscale";
    params.wmode = "透明";
    params.allowScriptAccess = "始终";
    
    var 属性 = {};
    属性.id = "${swf}";
    
    swfobject.embedSWF("${swf}.swf", "flashDiv", "${width}", "${height}", "9.0.0", "", flashvars, params, 属性);
    
  2. 将此用于 HTML:

    <前><代码><正文>

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

    //调用FlexExternalInterface所需的函数
    函数 thisMovie(电影名称) 
    {
        if (navigator.appName.indexOf("Microsoft") != -1) 
        {
            返回窗口[电影名称];
        } 
        别的 
        {
            返回文档[电影名称];
        }
    }
    
  4. 调用 Flash 方法:

    函数callFlashMethod()
    {
        thisMovie("${swf}").js_method_to_call();
    }
    

Here's something that should work really good:

  1. Use SWFObject.js for embedding the Flash content:

    // 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. Use this for the HTML:

    <body>
        <div id="flashDiv"></div>
    </body>
    
  3. To call your Flash method use this pattern:

    // Functions needed for calling Flex ExternalInterface
    function thisMovie(movieName) 
    {
        if (navigator.appName.indexOf("Microsoft") != -1) 
        {
            return window[movieName];
        } 
        else 
        {
            return document[movieName];
        }
    }
    
  4. Call the Flash method:

    function callFlashMethod()
    {
        thisMovie("${swf}").js_method_to_call();
    }
    
一紙繁鸢 2024-12-15 09:34:56

您将获得对嵌入式 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();

You get a reference to your embedded SWF object and use it to make a call to your as3 method.

//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();
这个俗人 2024-12-15 09:34:56

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

本页很好地描述了该解决方案,只需尝试使该示例正常工作即可。这样你就可以解决这个问题了,弗拉基米尔·茨维特科夫的回答就完整了。

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

This page describe the solution very well, just try to make that sample work. So you can sort out the problem, and Vladimir Tsvetkov's answer is complete.

夜未央樱花落 2024-12-15 09:34:56

我不确定这一行:

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

我会使用:

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

另外,我猜这只是粘贴到此处时的拼写错误,但是 flashFile.js_method_to_call; 应该是 flashFile.js_method_to_call() ;

I'm not sure about this line:

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

I would use:

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

Also, I'm guessing this was just a typo when pasting here, but flashFile.js_method_to_call; should be flashFile.js_method_to_call();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文