views:

9420

answers:

8

I have a function defined in AS3 that's gonna be called from client side via JavaScript. The AS3 functions simply print some value on SWF screen when it's called.

It works fine when I set an onclick event handler to a button and call the AS3 function. However I want to call this AS3 function as soon as the page loads. 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:

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

Then, I tried calling the by setting an onLoad event handler on the , but that also does not work - produces the same error.

So, my question is, how do I call an AS3 function automatically once page loads? In my project, I need to pass some client side initialization information to the flash once page loads.

A: 

The problem is that the Flash object is not initialized yet when the page finishes loading. It would probably be much safer to perform this initialization from within AS3. If you want to pass values from the HTML page, use flashVars.

David Hanak
+6  A: 

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.

In the flash:

ExternalInterface.call('flashReady');

In the page:

<script>
function flashReady() {
   ..
}
</script>
Mark Renouf
A: 

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

<div id="timewriter"><div>

You'll also be using the jQuery Timers plugin to time your loading. After this preparation the things will go very easy. The following piece of code will go in your $(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. Also my solution is based on jQuery javascript library.

Bogdan Constantinescu
A: 

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.

Tweakt is right, but upon calling the function in the javascript, have that call the javascript function that calls back to your swf; This way we know flash is ready as it sent the first call.

Brian Hodge blog.hodgedev.com hodgedev.com

Brian Hodge
+1  A: 

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.addDomLoadEvent(function() {
   $("#swfobject").get(0).inited('you are loaded!'); 
});
Scott Evernden
I think the addLoadEvent() is more correct. I believe the addDomLoadEvent() fires once the DOM is loaded but before any images,css, and flash objects are loaded. I did a test to confirm this.
dan
A: 

Answers by both tweakt and Bogdan are viable. Use tweakt's method if you have access to the Actionscript. Use Bogdan's if you don't. 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. Events are mentioned here: http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html But noone seems to know how to use them.

Jonathan Hawkes
A: 

hi there, 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:

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

if you are running on local.

:P

Good observation.
Stephan Kristyn
A: 

For the sake of completion, you would also have to use import flash.external.*; to make everything work.

Stephan Kristyn