THE PASZ.COM BLOG

Sunday, November 13, 2005

Fun with External Interface

So I finally had some time to start playing around with Flash 8. Here's a demo I built that shows the potential power of the ExternalInterface feature. One of the limitations of Flash has always been that the developer is "boxed into" the flash area of the page. There have always been tools to work around this problem, but ExternalInterface makes things so much easier because you can call ActionScript functions directly from JavaScript, and even pass parameters.

My demo sets up a mouseMove event handler, and passes the current x,y coordinates into Flash. Flash, in turn, uses the coordinates to adjust the rotation of the arrow clip so it's always pointing at the cursor. I was impressed by how smooth the animation is, which suggests that communications via ExternalInterface are very fast. (It would be interesting to do a comparison using "old school "data exchange techniques like SetVariable.)

There are a couple of limitations in the current demo. First of all, when you actually move the cursor into the flash area, callbacks stop working. Also, the movement of the arrow is restricted to a 90 degree slice. It would take some additional calculation to create 360 degrees of rotation.

I'll try to post the code later this week.

Credits
There's a nice tutorial on actionscript.com that helped me figure out how to set up a callback to ActionScript.
Codelifter.com has good (if slightly dated) code for determining the x-y position in JavaScript.
Finally, the original inspiration for this project came from reading Chapter 3 of Foundation Actionscript Animation by Keith Peters.

9 Comments: