ExternalInterface.addCallback为AS3不起作用不起作用、ExternalInterface、addCallback

2023-09-08 13:56:30 作者:老鼠扛刀上街找猫

我要打电话从JS功能。

I want to call AS function from JS.

我有以下的ActionScript 3 code:

I have the following ActionScript 3 code:

package  {
    import flash.display.*;
    import flash.events.*;
    import flash.system.*;
    import flash.external.ExternalInterface;
    public class Main extends Sprite {
        public function Main() 
        {
            ExternalInterface.addCallback("PlaySound", PlaySound);
        }
        public function PlaySound():void
        {

        }
    }
}

我需要从JavaScript调用函数PlaySound()。我尝试这样做以下列方式:

I need to call function PlaySound() from JavaScript. I try to do it in the following way:

function thisMovie(movieName) {
   if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
   } else {
        return document[movieName];
   }
}

function m()
{
  var obj=thisMovie("Main");
  obj.PlaySound();
}

但OBJ没有方法PlaySound()(obj不为null)。

But obj has no method PlaySound() (obj is not null).

怎么了?

推荐答案

我用它来寻找电影。它似乎是更可靠的:

I use this to find the movie. It seems to be more reliable:

function thisMovie(movieName) {
    var movie;
    try
    {
        movie = document[movieName];
        movie = (movie == null) ? window[movieName] : movie;        
    }
    catch (e)
    {
        return null;
    }
    return movie;
}

我还发现,ExternalInterface的不从本地文件系统中运行时正常工作。你试过从网络服务器上运行呢?

I've also found that ExternalInterface does not work properly when running from a local filesystem. Have you tried running this from a webserver?

这也有可能是你所看到的竞争条件......也许你正在尝试调用PlaySound之前就已经被注册为回调。如果你做了呼叫前等待一点点,会发生什么?

It's also possible that you are seeing a race condition... perhaps you are trying to call PlaySound before it has been registered as a callback. What happens if you wait a little bit before making the call?