为什么我的ActionScript3不调用我的JavaScript函数?

时间:2011-02-28 19:04:50

标签: javascript flash actionscript-3 actionscript

我有一些播放影片剪辑的动作。当影片剪辑结束时,我希望Javascript从页面中删除Flash对象。我似乎无法让它发挥作用。

当我在Flash中测试actionscript时,我没有得到任何编译错误,并且我的Traces都在我期望的时候执行。我也没有得到任何javascript错误,认为RemoveFlash()函数永远不会被调用。

这是我的ActionScript3:

import fl.video.*;
import flash.external.ExternalInterface;

MyPlayer.addEventListener(VideoEvent.COMPLETE, completePlay);

MyButton.addEventListener(MouseEvent.MOUSE_DOWN, interruptPlay);

function completePlay(e:VideoEvent):void
{
    trace("video completed");
    ExternalInterface.call("RemoveFlash");
}

function interruptPlay(e:MouseEvent):void
{
    trace("video interrupted");
    MyPlayer.stop();
    ExternalInterface.call("RemoveFlash");
}

这是我的JS:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script language="javascript" type="text/javascript" src="/Scripts/jquery-1.5.min.js"></script>
    <script type="text/javascript" src="/Scripts/swfobject2.js"></script>

    <script type="text/javascript">
        $(document).ready(function(){

            // Remove GreenPlayer
            function RemoveFlash()
            {
                alert("remove");
                $("#GreenPlayer").remove();
            }

            // add greenscreen swf
            var flashvars = {};
            flashvars.AllowScriptAccess="always";

                    var params = {};
            params.wmode = "transparent";
            params.AllowScriptAccess = "always";

            swfobject.embedSWF("/swf/GreenPlayer2.swf", "GreenPlayer", "200", "400", "8.0.0", '', flashvars, params);

        });
    </script>
</head>
<body>
        <div id="GreenPlayer">asd</div>
</body>
</html>

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

也许RemoveFlash()函数超出了swf的范围,因为你有一个匿名函数。尝试将RemoveFlash()函数移动到全局范围($(document).ready之外)并查看它是否有帮助。

答案 1 :(得分:1)

你的RemoveFlash函数是你的ready处理程序的本地函数,flash正在尝试调用一个名为RemoveFlash()的全局函数。将它移到负载处理程序之外,它会工作......

<script type="text/javascript">
    function RemoveFlash() {
       $("#GreenPlayer").remove();
    }      

    $(document).ready(function(){
        // add greenscreen swf
        var flashvars = {AllowScriptAccess: "always"};
        var params = {
           wmode: "transparent",
           AllowScriptAccess: "always"
        };
        swfobject.embedSWF("/swf/GreenPlayer2.swf", "GreenPlayer", "200", "400", "8.0.0", '', flashvars, params);

    });
</script>