as3ExternalInterface.addCallback 无法正常工作
我试图从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于,当您尝试调用 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.
使用此代码获取 swf 对象。
我在以下设备上测试了此代码:
并且运行良好。
Use this code to get swf Object.
I tested this code on:
and it worked fine.
成功总结:
我正在使用
AC_RunActiveContent.js
,这是 Flash 在发布时设置的。我的 swf 名为
fvar_js
,如下所示:我强调这一点是因为我从来不需要使用像
thisMovie
这样的函数在上面的帖子中指向 swf 对象。 我能够使用
fvar_js
直接(好吧,有点像你将看到的)。在我的 as3 代码中,我有这样几行:
其中 js_from_as_f 是一个更改文本字段中文本的函数。
在 HTML 中,我设置了以下内容:
我尝试了 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: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:
where
js_from_as_f
was a function that changed the text in a textfield.In the HTML I set up the following:
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.