简体   繁体   English

在页面加载时调用 flash 外部接口 AS3 function

[英]Calling a flash external interface AS3 function on page load

I have a function defined in AS3 that's gonna be called from client side via JavaScript.我在 AS3 中定义了一个 function,它将通过 JavaScript 从客户端调用。 The AS3 functions simply print some value on SWF screen when it's called. AS3 函数在调用时只是在 SWF 屏幕上打印一些值。

It works fine when I set an onclick event handler to a button and call the AS3 function.当我将 onclick 事件处理程序设置为按钮并调用 AS3 function 时,它工作正常。 However I want to call this AS3 function as soon as the page loads.但是,我想在页面加载后立即调用此 AS3 function。 I am using jQuery in the project and I placed the call to the AS3 function inside $(document).ready(), but that gives me the following error in FF2 firebug:我在项目中使用 jQuery 并在 $(document).ready() 中调用了 AS3 function,但这在 FF2 firebug 中给出了以下错误:

getFlashMovie("my_movie_name").my_as3_function is not a function getFlashMovie("my_movie_name").my_as3_function 不是 function

Then, I tried calling the by setting an onLoad event handler on the, but that also does not work - produces the same error.然后,我尝试通过在 上设置 onLoad 事件处理程序来调用 ,但这也不起作用 - 产生相同的错误。

So, my question is, how do I call an AS3 function automatically once page loads?所以,我的问题是,如何在页面加载后自动调用 AS3 function? In my project, I need to pass some client side initialization information to the flash once page loads.在我的项目中,一旦页面加载,我需要将一些客户端初始化信息传递给 flash。

You'll need to have your flash call a function in the page to notify it that the Flash is loaded and initialized, then use that as your entrypoint.您需要让您的 flash 在页面中调用 function 以通知它 Flash 已加载并初始化,然后将其用作您的入口点。

In the flash:在 flash 中:

ExternalInterface.call('flashReady');

In the page:在页面中:

<script>
function flashReady() {
   ..
}
</script>

If you use swfObject to embed your SWF (probably a good idea anyway) then you can use its addDomLoadEvent() function which allows you to do something once the SWF is fully loaded如果您使用swfObject嵌入您的 SWF(无论如何可能是一个好主意),那么您可以使用它的 addDomLoadEvent() function 它允许您在 SWF 完全加载后执行某些操作

swfobject.addDomLoadEvent(function() {
   $("#swfobject").get(0).inited('you are loaded!'); 
});

i am not trying to be a wiseguy here but do you test your work on a server?我不想在这里成为一个聪明人,但你会在服务器上测试你的工作吗?

external interface, addcallback dose not work on local filesystem, and eventually you may have to add:外部接口,addcallback 在本地文件系统上不起作用,最终您可能必须添加:

flash.system.Security.allowDomain(' http://localhost '); flash.system.Security.allowDomain(' http://localhost ');

if you are running on local.如果您在本地运行。

:P :P

For the sake of completion, you would also have to use import flash.external.*;为了完成,您还必须使用import flash.external.*; to make everything work.使一切正常。

The problem is that the Flash object is not initialized yet when the page finishes loading.问题是当页面完成加载时 Flash object 尚未初始化。 It would probably be much safer to perform this initialization from within AS3.从 AS3 中执行此初始化可能会更安全。 If you want to pass values from the HTML page, use flashVars.如果要从 HTML 页面传递值,请使用 flashVars。

I ran into this problem myself a couple of weeks ago.几周前我自己遇到了这个问题。 The solution is pretty simple:)解决方案非常简单:)

First, you need to put in your DOM a div首先,您需要在 DOM 中放入一个 div

<div id="timewriter"><div>

You'll also be using the jQuery Timers plugin to time your loading.您还将使用jQuery 计时器插件来计时加载。 After this preparation the things will go very easy.经过这样的准备,go 的东西就很容易了。 The following piece of code will go in your $(document).ready();以下代码将 go 在您的 $(document).ready();

var movie = getFlashMovie('my_movie_name');

if(movie.PercentLoaded() != 100)
{
    $("#timewriter").everyTime(100, function () 
    {
        if(movie.PercentLoaded() == 100)
        {

            $("#timewriter").stopTime();
            //the movie is loaded, call here your functions; usually this happens if you don't use cache

        }
    });
}
else
{
    //the movie is loaded, call here your functions; usually you get here if you use cache
}

Later edit: be careful that HTML page load doesn't mean the swf was loaded, that happens right after the web page load complete event.稍后编辑:注意 HTML 页面加载并不意味着 swf 已加载,这发生在 web 页面加载完成事件之后。 Also my solution is based on jQuery javascript library.我的解决方案也是基于jQuery javascript 库。

It seems like the collection of answers offered answers this closest to it's entirety.似乎答案集合提供了最接近其全部的答案。

As David Hanak said, the flash object cannot be accessed yet because it is initializing, though i disagree that we must rely on flashvars, though I love them.正如大卫哈纳克所说,flash object 尚无法访问,因为它正在初始化,尽管我不同意我们必须依赖 flashvars,尽管我喜欢它们。

Tweakt is right, but upon calling the function in the javascript, have that call the javascript function that calls back to your swf; TWEAKT是正确的,但是在拨打ZC1C425268E68385D1AB5074C174C174F14Z中,ZDE9B9B9B9ED78D7E2EFEEFFEE780E2F9191STEXPEFEFF9191 FARSEDENSED wald wall the ZDE9B9B9B9B9ED78D7E2E2E2E2E21912E2197888019788078807880788078807880788078807880788078807807880E7807780780780780780780780年。 This way we know flash is ready as it sent the first call.这样我们就知道 flash 在发送第一个呼叫时已准备就绪。

Answers by both tweakt and Bogdan are viable. Tweakt 和 Bogdan 的答案都是可行的。 Use tweakt's method if you have access to the Actionscript.如果您可以访问 Actionscript,请使用tweakt 的方法。 Use Bogdan's if you don't.如果你不这样做,请使用 Bogdan's。 I was looking for an alternative besides polling (when you don't have access to the Actionscript) but I have been unsuccessful in finding one thus far.我一直在寻找除了轮询之外的替代方法(当您无法访问 Actionscript 时),但到目前为止我一直没有找到一个。 Events are mentioned here: http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html But noone seems to know how to use them.这里提到了事件: http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html但似乎没有人知道如何使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM