如何使Flash CS3,ActionScript将事件发送到JavaScript?

时间:2020-03-05 18:46:07  来源:igfitidea点击:

我正在使用Flash在我的网站上播放.flv movieclip,但是我想让.swf发送在开始加载,开始播放和结束播放时在我的javascript中触发一个事件。

使用ActionScript 3.0在Flash CS3中执行此操作的最佳方法是什么?

解决方案

回答

常用的方法是使用ExternalInterface类,我们可以使用该类来调用JavaScript方法。

首先定义JavaScript方法,例如:

<script language="JavaScript">
    function startsPlaying()
    {
        // do something when the FLV starts playing
    }
</script>

然后修改ActionScript以在适当的时候调用JavaScript方法:

// inform JavaScript that the FLV has started playing
ExternalInterface.call("startsPlaying");

有关更多信息,请参见相关的Flash CS3文档。

回答

我们需要在HTML中使用" allowScriptAccess" Flash变量。我们可能要使用" sameDomain"作为类型。请注意,如果我们要跨域访问,还需要在服务器上托管一个名为" crossdomain.xml"的特殊文件,该文件可启用此类脚本编写(Flash Player将对此进行检查。有关更多信息,请访问http://kb.adobe。 com / selfservice / viewContent.do?externalId = tn_14213&sliceId = 2

通话很简单。 :-)在Flash代码中,我们将使用ExternalInterface进行调用,如此处所述:

http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001655.html

简短版:我们说

ExternalInterface.call("javascriptFunction", "argument")

回答

如果我们不想加载

import flash.external.*;

所以你也可以做一个

getUrl("javascript:startsPlaying();");