Flash / JS communication: some solutions
Thursday 9 June 2005 – Filed under: Flash – No Comments
A few days ago Macromedia released their Flash / JavaScript Integration Kit (currently in beta). I’ve been working on reliable and flexible Javascript to Flash events and vice versa myself. I already developed a way of queueing getURL calls from Flash to Javascript. Today I’m releasing a method of queueing calls from JavaScript to Flash.
So what’s this queueing all about? It all comes down to the speed of the DOM (or lack thereof) and thus the need of giving the browser enough time to catch its breath in between calls. And this not only applies to calls from Flash to the outside world, but also to calls made by Javascript to your Flash application.
In short, these are possible solutions to the issues described above:
- Queue getURL calls from Flash to the browser.
- Queue calls from Javascript to Flash.
I already posted a solution for the first issue, namely in the form of the PostCenter class. Read that blog entry for more information on how to implement getURL queueing in your applications.
With that issue tackled, lets move over to the next problem, queueing calls from JS to Flash. To be honest, I didn’t know that those calls made are also dropped when they’re made quickly in succession, but one of my blog visitors, Cosmin Cimpoi, brought the issue to my attention.
So I’ve been working on a way to make Javascript wait a certain number of milliseconds before sending the next event to Flash. I quickly discovered that using setInterval in Javascript is quite a different story from setInterval in Flash (in JS, you’re loosing local scope), but a coworker of mine, who fluently speaks JS, came up with a nice solution (thanks ReD!). Apart from surprises such as this, I managed to put together a cross browser (successfully tested in WinXP IE and Firefox, as well as Mac OS X Safar, that is) piece of Javascript that manages calls to your Flash application. After some testing, I settled for a delay of 500 milliseconds, quite a long timeout, but that seems to give the best results.
I’m not sure how often one is going to send a lot of simultaneous events from Javascript to Flash, but if you do, this may be a solution worth trying.
I have posted an example online here. You can also download the source files. Please note that the source files use my ExternalFunction class to receive and manage Javascript events in Flash. Read the related blog entry if you’re not sure what the ExternalFunction class is all about.
One final remark: I know this is not a perfect solution. Especially on very slow machines, the delay may be too short, resulting in dropped events. The best solution would be a confirmation event from Flash back to Javascript, indicating the event was received successfully, so JS can send the next event.
« previously: Queueing getURL calls next: Project: Hi Bling Bling »
Leave a Reply
Recent Articles
- New bike
- FLV Player 2.0.25 released
- No custom headers for Flash file upload. Why?
- Building AIR with FDT
- Adobe MAX 2007
- FLV Player 2.0 released
- The new Photoshop logo
- mProjector WindowResizer class
Advertisement
Categories
- Art & Design (4)
- Flash (37)
- FLV Player (9)
- Software (16)
- User Experience Design (3)
- Various (8)
- Work (6)