Section 23.5. Scripting Flash 8


23.5. Scripting Flash 8

Version 8 of the Flash Player includes a class named ExternalInterface that revolutionizes Flash scripting by greatly simplifying JavaScript-to-Flash communication and Flash-to-JavaScript communication. ExternalInterface defines a static call() function for calling named JavaScript functions and obtaining their return values. It also defines a static addCallback() method for exporting ActionScript functions for use by JavaScript. ExternalInterface is documented in Part IV.

To demonstrate the ease of scripting with ExternalInterface, let's convert Examples 23-4 and Examples 23-5 to use it. Example 23-6 lists the converted ActionScript code, and Example 23-7 shows the converted JavaScript code (the <object>, <embed>, and <form> tags are not altered from Example 23-5 and are omitted here).

The comments in these examples should tell you everything you need to know about using ExternalInterface. The ExternalInterface.addCallback() method is also demonstrated in Example 22-12.

Example 23-6. ActionScript using ExternalInterface

 /**  * Box2.as: ActionScript code to demonstrate JavaScript <-> Flash communication  *          using the ExternalInterface class of Flash 8.  *  * Compile this code using mtasc with a command like this:  *  *   mtasc -version 8 -header 300:300:1 -main -swf Box2.swf Box2.as  *  * If you use the Flash IDE instead, insert a call to Box.main( ) in the  * first frame of your movie.  */ import flash.external.ExternalInterface; class Box {     static function main( ) {         // Use the new External Interface to export our ActionScript function.         // This makes it very easy to invoke the function from JavaScript,         // but it is only supported by Flash 8 and later.         // The first argument of addCallback is the name by which the function         // will be known in JavaScript. The second argument is the         // ActionScript object on which the function will be invoked. It         // will be the value of the 'this' keyword. And the third argument         // is the function that will be called.         ExternalInterface.addCallback("drawBox", null, function(x,y,w,h) {                                           _root.beginFill(0xaaaaaa, 100);                                           _root.lineStyle(5, 0x000000, 100);                                           _root.moveTo(x,y);                                           _root.lineTo(x+w, y);                                           _root.lineTo(x+w, y+h);                                           _root.lineTo(x, y+h);                                           _root.lineTo(x,y);                                           _root.endFill( );                                           return w*h;                                       });         // This is an ActionScript event handler.         // Tell JavaScript about mouse clicks using ExternalInterface.call( ).         _root.onMouseDown = function( ) {             ExternalInterface.call("reportMouseClick",                                    _root._xmouse, _root._ymouse);         }         // Tell JavaScript that we're fully loaded and ready to be scripted.         ExternalInterface.call("flashReady");     } } 

Example 23-7. Simplified Flash scripting with ExternalInterface

 <script> // When an ActionScript function is exported with ExternalInterface.addCallback, // we can just call it as a method of the Flash object. function draw( ) {     var flash = window.movie || document.movie; // Get Flash object     return flash.drawBox(100, 100, 100, 50);    // Just invoke a function on it } // These are functions Flash will call with ExternalInterface.call( ). function flashReady( ) { document.f.button.disabled = false; } function reportMouseClick(x, y) { alert("click: " + x + ", " + y); } </script> 




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net