as3ExternalInterface.addCallback 无法正常工作

发布于 2024-07-10 02:42:53 字数 1433 浏览 6 评论 0原文

我试图从 javascript 访问 swf,所以 livedocs 中的这个示例就是我想要修改的内容。 http://livedocs.adobe.com/flash/ 9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html#includeExamplesSummary

但是,由于某种原因它无法正常工作。 我遇到的问题 是它在 Safari 和 Firefox 中不起作用,只有当我发出警报时它才起作用 在 javascript 之前的函数中将值传递给 swf。 (看来还需要一些时间) 我也尝试在as3中设置计时器,但是计时器不起作用,只有js中的警报有帮助。

我想做的就是使用js告诉swf文件播放ep1.swf。 这是我的 js 代码:

document.observe('dom:loaded', function() {
    $('episode1').observe('click', function() {
        var params = {wmode : "transparent", allowScriptAccess:"always", movie:"header"};
        swfobject.embedSWF("swf/float.swf", "header", "100%", "100%", "9.0.0","expressInstall.swf", "", params, "");
        sendToActionScript("ep1.swf");
    });
})
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        //alert("aaa")
        return document[movieName];
    }
}
function sendToActionScript(value) {     
    thisMovie('header').sendToActionScript(value);       
}

这是我的 as3 代码:

private function receivedFromJavaScript(value:String):void {

    loader.load(new URLRequest(value));

}

我已经尝试了很长时间,有人知道如何解决这个问题吗? 谢谢。

I was trying to access swf from javascript, so this example in livedocs is what I'm trying to modify. http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/external/ExternalInterface.html#includeExamplesSummary

However,it is not working correctly for some reason. The problem I'm encountering
is that it does not work in Safari and in Firefox, it only works if I put an alert
in the function before javascript pass the value to swf. (seems like it needs some time)
I also tried to set a timer in as3, but timer doesn't work, only alert in js helps.

All I wanted to do is use js to tell the swf file to play ep1.swf.
Here's my js code:

document.observe('dom:loaded', function() {
    $('episode1').observe('click', function() {
        var params = {wmode : "transparent", allowScriptAccess:"always", movie:"header"};
        swfobject.embedSWF("swf/float.swf", "header", "100%", "100%", "9.0.0","expressInstall.swf", "", params, "");
        sendToActionScript("ep1.swf");
    });
})
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        //alert("aaa")
        return document[movieName];
    }
}
function sendToActionScript(value) {     
    thisMovie('header').sendToActionScript(value);       
}

Here's my as3 code:

private function receivedFromJavaScript(value:String):void {

    loader.load(new URLRequest(value));

}

I've been trying for a really long time, does anyone know how to fix this? Thanks.

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

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

发布评论

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

评论(3

剩余の解释 2024-07-17 02:42:53

问题在于,当您尝试调用 SWF 文件时,该文件尚未完全加载。 Flash 播放器可能已加载,但加载和初始化 swf 文件需要一段时间。

您需要做的是在加载时从 SWF 文件调用 javascript 函数,并将您的 javascript 放在那里,而不是像您现在所做的那样放在页面加载处理程序中。 这样您就知道您的闪存应用程序此时已正确初始化。 您正在使用的ExternalInterface 类具有让您回调JavaScript 的方法。

The problem is that the SWF file isn't fully loaded by the time you try to call it. The flash player is probably loaded but it takes a while to load and initialise the swf file.

What you need to do is make a call from the SWF file to a javascript function when it's loaded and put your javascript there rather than in the page loaded handler that you seem to be doing now. That way you know that your flash application is properly initialized by then. The ExternalInterface class you are using has methods to let you call back into the javascript.

心作怪 2024-07-17 02:42:53

使用此代码获取 swf 对象。

我在以下设备上测试了此代码:

  • IE 9,8,7
  • Firefox 6.0.1
  • Netscape Navigator 9.0.0.6
  • Opera 11.5
  • Google chrome 13.0.782.215
  • Safari 3.2(全部在 Windows 操作系统中)

并且运行良好。

function GetSWF(strName) {
    if (window.document[strName] != null) {
        if (window.document[strName].length == null)
            return window.document[strName];
        else
            return window.document[strName][1];
    } else {
        if (document[strName].length == null)
            return document[strName];
        else
            return document[strName][1];
    }
}

Use this code to get swf Object.

I tested this code on:

  • IE 9,8,7
  • Firefox 6.0.1
  • Netscape Navigator 9.0.0.6
  • Opera 11.5
  • Google chrome 13.0.782.215
  • Safari 3.2 (All In Windows OS)

and it worked fine.

function GetSWF(strName) {
    if (window.document[strName] != null) {
        if (window.document[strName].length == null)
            return window.document[strName];
        else
            return window.document[strName][1];
    } else {
        if (document[strName].length == null)
            return document[strName];
        else
            return document[strName][1];
    }
}
决绝 2024-07-17 02:42:53

成功总结:


我正在使用 AC_RunActiveContent.js,这是 Flash 在发布时设置的。
我的 swf 名为 fvar_js,如下所示:

AC_FL_RunContent(
        ...
    'src', 'fvar_js',
        ...

我强调这一点是因为我从来不需要使用像 thisMovie 这样的函数
在上面的帖子中指向 swf 对象。 我能够使用
fvar_js 直接(好吧,有点像你将看到的)。


在我的 as3 代码中,我有这样几行:

if (ExternalInterface.available) {
    ExternalInterface.addCallback("js_to_as_f", js_from_as_f);
}

其中 js_from_as_f 是一个更改文本字段中文本的函数。


在 HTML 中,我设置了以下内容:

var timeoutId;
var js_initiate_callback = function() {
    // This is the swf object:
    fvar_js.js_to_as_f();
    clearTimeout ( timeoutId );
}
var reset_fvar_f = function(new_val) {
    fvar_val = new_val;
}
//js_initiate_callback();
timeoutId = setTimeout(js_initiate_callback, 1000);

我尝试了 1 毫秒和 100 毫秒,但结果参差不齐。 1000毫秒
这可以在 PC 上的 IE、FF 和 Safari 中运行。 没有在 Mac OS X 上检查过。

显然,关键是允许所有对象和所有对象连接
是时候进行设置了。 我不知道最短时间是多少。

Summary of a success:


I am using AC_RunActiveContent.js, as set up by Flash when published.
My swf is called fvar_js, as seen below:

AC_FL_RunContent(
        ...
    'src', 'fvar_js',
        ...

I STRESS this because I NEVER had to use a function like thisMovie
in the post above to point to the swf object. I was able to use
fvar_js straightaway (well, sort of, as you shall see).


In my as3 code I had the lines:

if (ExternalInterface.available) {
    ExternalInterface.addCallback("js_to_as_f", js_from_as_f);
}

where js_from_as_f was a function that changed the text in a textfield.


In the HTML I set up the following:

var timeoutId;
var js_initiate_callback = function() {
    // This is the swf object:
    fvar_js.js_to_as_f();
    clearTimeout ( timeoutId );
}
var reset_fvar_f = function(new_val) {
    fvar_val = new_val;
}
//js_initiate_callback();
timeoutId = setTimeout(js_initiate_callback, 1000);

I tried 1ms and 100ms, but the results were spotty. With 1000ms
this worked in IE, FF and Safari on a PC. Have not checked on Mac OS X.

The key, evidently, is to allow all objects and all object connections
time to get set up. I have no idea what the minimum time is.

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