The Local Connect component creates a bi-directional data connection to other Local Connect components running on the same client machine, so that multiple assets can communicate with each other.
For more information, see the Local Connect component.
// Setup: localConnectComponent is the instance name of the // Local Connect component on the stage. import com.google.ads.studio.localconnect.LocalConnectParent; import com.google.ads.studio.localconnect.LocalConnectWrapper; var dataObject:Object = {message: "Start synced animation!"}; // Send data to all connected components. localConnectComponent.sendData(dataObject); // Send data to parent (from a child). localConnectComponent.sendData( dataObject, LocalConnectParent.PARENT_NAME); // Send data to a specific child. localConnectComponent.sendData(dataObject, "child");
import com.google.ads.studio.events.StudioEvent; import com.google.ads.studio.localconnect.LocalConnectParent; var dataHandler:Function = function(event:StudioEvent):void { trace("Message was " + event.data.message); // If the dataObject referred to in the above "send" example is sent, // event.data.message will be "Start synced animation!". if (event.sender == LocalConnectParent.PARENT_NAME && event.data.message == "Start synced animation!") { trace("Message was sent from the parent"); // Start your synced animation here. } }; localConnectComponent.addEventListener( StudioEvent.DATA_RECEIVED, dataHandler);
// Creating a parent. import com.google.ads.studio.localconnect.LocalConnectParent; var childChannelNames:Array = ["my child"]; var parentConnect:LocalConnectParent = new LocalConnectParent(childChannelNames); parentConnect.connect(); // Creating a child. import com.google.ads.studio.localconnect.LocalConnectChild; var childConnect:LocalConnectChild = new LocalConnectChild("my child"); childConnect.connect();
Events
Name | Type | Description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
CONNECT | com.google.ads.studio.events.StudioEvent | Dispatched when the component establishes a connection. import com.google.ads.studio.events.StudioEvent; var connectHandler:Function = function(event:StudioEvent):void { trace("Connection succeeded."); }; lcComponentInstance.addEventListener(StudioEvent.CONNECT, connectHandler); | ||||||||
DATA_RECEIVED | com.google.ads.studio.events.StudioEvent | Dispatched when data is received from the connection. The properties of the event object have the following values:
import com.google.ads.studio.events.StudioEvent; var dataReceivedHandler:Function = function(event:StudioEvent):void { trace("Intended data recipient is " + event.targetChannel + "."); trace("Sender is " + event.sender + "."); trace("Message is " + event.data + "."); }; lcComponentInstance.addEventListener( StudioEvent.DATA_RECEIVED, dataReceivedHandler); | ||||||||
FAIL | com.google.ads.studio.events.StudioEvent | Dispatched when the component has failed to initialize. | ||||||||
INIT | com.google.ads.studio.events.StudioEvent | Dispatched when the component has initialized.
This event may fire almost immediately after component construction. To
ensure capturing the initialized state please refer to the example:
// Note the variable component refers to the component instance. import com.google.ads.studio.events.StudioEvent; var initializedHandler:Function = function( event:StudioEvent = null):void { trace("The component initialized!"); }; if (component.isInitialized()) { initializedHandler(); } else { component.addEventListener(StudioEvent.INIT, initializedHandler); } | ||||||||
TIMEOUT | com.google.ads.studio.events.StudioEvent | Dispatched when the timeout is reached when attempting to establish a connection. import com.google.ads.studio.events.StudioEvent; var timeoutHandler:Function = function(event:StudioEvent):void { trace("Connection failed."); }; lcComponentInstance.addEventListener(StudioEvent.TIMEOUT, timeoutHandler); |
Constructors
LocalConnectWrapper()
:
void
Creates a LocalConnectWrapper.
Instance Methods
Defined in: com.google.ads.studio:EnabledComponent
getEnabler()
:
Object
isInitialized()
:
Boolean
Returns whether the component has initialized.
Defined in com.google.ads.studio.localconnect:LocalConnectWrapper
sendData(data:Object, target:String=null)
:
void
Sends data to one or more other Local Connect components.
import com.google.ads.studio.localconnect.LocalConnectParent; import com.google.ads.studio.localconnect.LocalConnectWrapper; var sendClickHandler:Function = function(event:MouseEvent):void { var dataObject:Object = {message: "Hello World!"}; // Send data to all connected components. lcComponentInstance.sendData(dataObject); // Send data to parent (from a child). lcComponentInstance.sendData( dataObject, LocalConnectParent.PARENT_NAME); // Send data to a specific child. lcComponentInstance.sendData(dataObject, "lc_child"); }; sendButton.addEventListener(MouseEvent.CLICK, sendClickHandler);