Packageflx.external
Classpublic class FlashInterface

Author: Robert Taylor
Version: 2.1.0
Created: 28 Oct 2006
Last Updated: 03 Dec 2006
Website: http://www.flashextensions.com
Documentation: http://www.flashextensions.com/products/flashinterface.php
Description: Provides the means to communicate directly between the Flash 8 and 9 ActionScript Virtual Machines (AVM).

FlashInterface AS2 Class

FlashInterface AS3 Class Flash 9 player has been developed with a new ActionScript Virtual Machine (AVM). Versions of the Flash player 8 and lower run under the first AVM. The two virtual machines do not support direct communication back and forth with each other. FlashInterface establishes a means for the two virtual machines to communicate directly with each other, both synchronously and asynchronously.

Flash and Flex Solutions



Public Methods
 MethodDefined by
  
addEventListener(event:String, listener:Object):void
[static] Method; registers a listener object with a component instance that is broadcasting an event.
FlashInterface
  
alert(... args):void
[static] Method; used for debugging and providing error messages.
FlashInterface
  
call(path:String, ... args):Object
[static] Method; invokes synchronous calls to public methods and properties of registered controls and receives and returns values.
FlashInterface
  
dispatchEvent(eventObject:Object):void
[static] Method; dispatches an event to any listener registered with an instance of the class.
FlashInterface
  
publish(root:DisplayObject, makePublic:Boolean = false):void
[static] Method; instantiates the needed JavaScript and EventDispatcher in order to perform any dispatching and/or calls to other SWFs.
FlashInterface
  
register(id:String, target:Object, overwrite:Boolean = false):Boolean
[static] Method; sets a control so it can be accessed through the call method.
FlashInterface
  
removeEventListener(event:String, listener:Object):void
[static] Method; unregisters a listener object from a FlashInterface instance that is broadcasting an event.
FlashInterface
  
unregister(id:String, target:Object):Boolean
[static] Method; removes a control from being accessed through the call method.
FlashInterface
Events
 EventSummaryDefined by
  
*
When subscribing as an event listener, the event type is FlashInterfaceEvent.FlashInterface
Method detail
addEventListener()method
public static function addEventListener(event:String, listener:Object):void

Method; registers a listener object with a component instance that is broadcasting an event. When the event occurs, the listener object or function is notified. FlashInterface uses the EventDispatcher provided by the Flash framework. You many look up the documentation regarding its usage for any specific details.

   import flx.events.FlashInterface;
   FlashInterface.addEventListener("message", messageHandler);
   
   function messageHandler(evt:Object)
   {
     FlashInterface.alert("You said", evt.data);
   }
   

Parameters
event:String — a string that is the name of the event.
 
listener:Object — a reference to a listener object or function.
alert()method 
public static function alert(... args):void

Method; used for debugging and providing error messages.

Parameters
... args — any number of parameters may be passed in. Each parameter will be divided with a space.
call()method 
public static function call(path:String, ... args):Object

Method; invokes synchronous calls to public methods and properties of registered controls and receives and returns values.

The call function is power and easy to use. It allows you to communication and control another SWFs properties and functions as if they both resided inside the same AVM or scope. To communicate with another SWF, you reference it by its assigned id. Ids are used to reference a swf. Think of it as an instance name. Ids are typically assigned by the HTML page as an "id" or "name". However, when loading up a SWF inside another SWF, the loaded SWF by default takes on the id of the loader. To ensure that each item has its own unique id, we can assign it an id using the flashId property assignment. To assign a flashId do one of the following:

1) URL parameter - (i.e. myflashapp.swf?flashId=myFlashId) [recommended method of implementation]
2) Flash var - (i.e. SWFObject example - setVariable("flashId", "myFlashId")
3) Internal - (i.e. _root.flashId = "myFlashId") - [not recommended because it hard-codes a flashId value inside of the Flash 8 SWF]

To talk to another SWF you reference it by its flashId as demonstrated in the example code below, including children that have been loaded inside a Flash 9 SWF. Lets say a loaded Flash 8 SWF was given the flashId of "flash". We would communicate with with the following code.

    import flx.events.FlashInterface;
    FlashInterface.call("flash.sendMessage", "Hello, world!");
   

In order for the loaded Flash 8 SWF to communicate with its "parent" Flash 9 SWF, reference the Flash 9 SWF as "root".

    import flx.events.FlashInterface;
    FlashInterface.call("root.sendMessage", "Hello, world!");
    

In the previous examples we demonstrated calling a function. You can also use call to communicate with properties, both for setting and retrieving data. Below is an example:

    import flx.events.FlashInterface;
    var info:Object = FlashInterface.call("flash._visible", false);
    trace(info.result) // returns false
    var info:Object = FlashInterface.call("flash._name");
    trace(info.result) // returns the instance name of the movieclip.
   

Parameters
path:String — path to the method or property.
 
... args — Any additional parameters you which to pass in the call

Returns
Object — Object containing information regarding the event call.

The return value comes back as an Object simlar to a call to a server using LoadVars in ActionScript 2.0 and a Loader / URL Request in ActionScript 3.0. I found this very helpful when trying to find out why the communication had failed, or if it was successful. The object contains the following information:

If successful:

   Object - 
     status: "success" (String)
     result: return value 
     target: SWF we are communicating with (String)
   

If failure:

   Object - 
     status: "error" (String)
     target: SWF we are communicating with (String)
     message: Error message indicating problem (String)
   

dispatchEvent()method 
public static function dispatchEvent(eventObject:Object):void

Method; dispatches an event to any listener registered with an instance of the class. This method is usually called from within a component's class file. FlashInterface uses the EventDispatcher provided by the Flash framework. You many look up the documentation regarding its usage for any specific details.

    import flx.events.FlashInterface;
    FlashInterface.dispatchEvent({type:"message", data:"Hello, world!"});
    

Parameters
eventObject:Object — A reference to an event object. The event object must have a type property that is a string indicating the name of the event. Generally, the event object also has a target property that is the name of the instance broadcasting the event. You can define other properties on the event object that help a user capture information about the event when it is dispatched.
publish()method 
public static function publish(root:DisplayObject, makePublic:Boolean = false):void

Method; instantiates the needed JavaScript and EventDispatcher in order to perform any dispatching and/or calls to other SWFs. This function must be called before any other actions within FlashInterface or an error will be thrown.

Parameters
root:DisplayObject — pointer to the Document root of the application. For AS 2.0 this is the _root. For AS 3.0, this is root.
 
makePublic:Boolean (default = false) — establishes whether the SWF can be accessed by other SWFs either directly or through a registration process. This property is in place for security. If you wish for other SWFs only to access your SWF through events, then set makePublic to false. If you wish to expose your public API to other SWFs, set makePublic to true.
register()method 
public static function register(id:String, target:Object, overwrite:Boolean = false):Boolean

Method; sets a control so it can be accessed through the call method. Public properties and methods may be accessed once a control has been registered.

Two examples are provided below. There is a subtle difference between using publis in AS 2.0 and AS3.0. When invoking publish, _root is referenced in AS 2.0, whereas root is referenced in AS 3.0. For details on using the publish function see documentation.

ActionScript 2.0 Example

    import flx.events.FlashInterface;
    FlashInterface.publish(_root, true);
    FlashInterface.register("demo", myMovieClip);
    
    myMovieClip.sendMessage = function(message:String):Void
    {
     FlashInterface.alert("You said", message);
    }
   

ActionScript 3.0 Example

    import flx.events.FlashInterface;
    FlashInterface.publish(root, true);
    FlashInterface.register("demo", myMovieClip);
    
    myMovieClip.sendMessage = function(message:String):Void
    {
     FlashInterface.alert("You said", message);
    }
   

Parameters
id:String — the unique identifier by which an item may be referenced.
 
target:Object — the control object being registered.
 
overwrite:Boolean (default = false) — overrides a previously registered control wil the same ID.

Returns
Boolean — Boolean returns if the registration was successful.
removeEventListener()method 
public static function removeEventListener(event:String, listener:Object):void

Method; unregisters a listener object from a FlashInterface instance that is broadcasting an event. FlashInterface uses the EventDispatcher provided by the Flash framework. You many look up the documentation regarding its usage for any specific details.

    import flx.events.FlashInterface;
    FlashInterface.removeEventListener("message", messageHandler);
    

Parameters
event:String — a string that is the name of the event.
 
listener:Object — a reference to a listener object or function.
unregister()method 
public static function unregister(id:String, target:Object):Boolean

Method; removes a control from being accessed through the call method.

    import flx.events.FlashInterface;
    FlashInterface.unregister("demo", myMovieClip);
    

Parameters
id:String — the id associated to the swf through html.
 
target:Object — the unique identifier by which an item may be referenced.

Returns
Boolean — Boolean returns if the unregistration was successful.
Event detail
*event 
Event object type: flx.events.FlashInterfaceEvent

When subscribing as an event listener, the event type is FlashInterfaceEvent. No event can be defined since this is handled anonymously through the ActionScript Virtual Machines (AVMs). FlashInterfaceEvent contains all properties and methods of Event and the property data which contains any data send from the dispatching object.