使用ExternalInterface从JavaScript调用ActionScript 3.0/Flash中的函数

发布于 2024-11-03 17:46:49 字数 1994 浏览 4 评论 0原文

即使确保将“allowScriptAccess”设置为始终,我也无法使其工作。我成功地将 Flash 影片放入浏览器中,并调用 ReceiveDataFromFlashMovie() 并打印“到达这里”,但根据 Internet Explorer 中的错误消息,GetFlashMovieObject() 似乎只返回 NULL。我错过了什么吗?

HTML 文件头:

<script type="text/javascript">
function getFlashMovieObject(movieName)
{
  if (navigator.appName.indexOf("Microsoft") != -1) { 
    return window[movieName] 
  } 
  else { return document[movieName] }
}

function ReceiveDataFromFlashMovie()
{
     document.write("Got here");
     var callResult = getFlashMovieObject("MakingButtons").myFunction();
     return callResult;
}

</script>

HTML:

<script type="text/javascript">
document.write("Hello World.")
ReceiveDataFromFlashMovie();
document.write(callResult)
</script>

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="500" height="500" id="MakingButtons" align="middle">
<param name="allowScriptAccess" value="always" />
<param name="movie" value="MakingButtons.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="MakingButtons.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="MakingButtons" align="middle" allowScriptAccess="always" swlliveconnect="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

ActionScript 3.0:

import flash.text.TextField;
import flash.external.*;
// The name of the Flash variable to be called in JavaScript
var flashFunction:String = "myFunction";
var instance:Object = null;

// Callback function executed by the name of variable
var realFunction:Function = callMe;
ExternalInterface.addCallback(flashFunction, realFunction);

var foo = "Goodbye!";
function callMe():String
{
    return foo;
}

谢谢!

I can't get this to work even after making sure to set "allowScriptAccess" to always. I successfully put the flash movie in the browser and call ReceiveDataFromFlashMovie() and print "Got here" but it seems like GetFlashMovieObject() only returns NULL according to an error message in Internet Explorer. Am I missing something?

Head of the HTML file:

<script type="text/javascript">
function getFlashMovieObject(movieName)
{
  if (navigator.appName.indexOf("Microsoft") != -1) { 
    return window[movieName] 
  } 
  else { return document[movieName] }
}

function ReceiveDataFromFlashMovie()
{
     document.write("Got here");
     var callResult = getFlashMovieObject("MakingButtons").myFunction();
     return callResult;
}

</script>

HTML:

<script type="text/javascript">
document.write("Hello World.")
ReceiveDataFromFlashMovie();
document.write(callResult)
</script>

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="500" height="500" id="MakingButtons" align="middle">
<param name="allowScriptAccess" value="always" />
<param name="movie" value="MakingButtons.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="MakingButtons.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="MakingButtons" align="middle" allowScriptAccess="always" swlliveconnect="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>

ActionScript 3.0:

import flash.text.TextField;
import flash.external.*;
// The name of the Flash variable to be called in JavaScript
var flashFunction:String = "myFunction";
var instance:Object = null;

// Callback function executed by the name of variable
var realFunction:Function = callMe;
ExternalInterface.addCallback(flashFunction, realFunction);

var foo = "Goodbye!";
function callMe():String
{
    return foo;
}

Thank you!

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

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

发布评论

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

评论(3

薄荷梦 2024-11-10 17:46:49

据我了解您的问题,您唯一要做的就是更改

顺序

<script type="text/javascript">
document.write("Hello World.")
ReceiveDataFromFlashMovie();
document.write(callResult)
</script>

对象嵌入代码之后的 :)。它没有找到它(返回未定义),因为它尚未嵌入。

As far as I understand your problem, the only thing you have to do is changing the order :)

put

<script type="text/javascript">
document.write("Hello World.")
ReceiveDataFromFlashMovie();
document.write(callResult)
</script>

after the object embed code. It didn't find it (returned undefined), because it was not yet embedded.

牛↙奶布丁 2024-11-10 17:46:49

如果您使用 swfObject 嵌入 Flash 影片,则可以使用 swfobject.getObjectById 来检测您的 swf 并调用您的方法。

swfobject.getObjectById("MakingButtons").myExternalMethod();

如果您不使用 swfObject,只需在 JS 代码上复制并粘贴 getObjectById 方法:

function getObjectById(objectIdStr) {
        var r = null;
        var o = getElementById(objectIdStr);
        if (o && o.nodeName == "OBJECT") {
            if (typeof o.SetVariable != UNDEF) {
                r = o;
            }
            else {
                var n = o.getElementsByTagName(OBJECT)[0];
                if (n) {
                    r = n;
                }
            }
        }
        return r;
    }

并通过执行以下操作来调用它:

getObjectById("MakingButtons").myExternalMethod();

看看这个,我实际上在我的博客上编写了一个小示例: http://www.nelsond8.com/?p=515

If you are using swfObject to embed your flash movie you can use swfobject.getObjectById to detect your swf and call your methods.

swfobject.getObjectById("MakingButtons").myExternalMethod();

If you are not using swfObject just copy and past the getObjectById method on your JS code:

function getObjectById(objectIdStr) {
        var r = null;
        var o = getElementById(objectIdStr);
        if (o && o.nodeName == "OBJECT") {
            if (typeof o.SetVariable != UNDEF) {
                r = o;
            }
            else {
                var n = o.getElementsByTagName(OBJECT)[0];
                if (n) {
                    r = n;
                }
            }
        }
        return r;
    }

And call it by doing:

getObjectById("MakingButtons").myExternalMethod();

Have a look on this, I actually code a small example on my blog: http://www.nelsond8.com/?p=515

我一向站在原地 2024-11-10 17:46:49

您不允许加载/初始化 SWF。
在访问 SWF 内的任何内容之前,您需要确保它已准备好处理回调。

You are not allowing for the SWF to load/initialize.

Before you can access anything inside the SWF you need to make sure its ready to handle callbacks.

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