13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Communicating with other Flash Player and AIR instances<br />

The first way to add callback methods is to ext<strong>en</strong>d the LocalConnection class. You define the methods within the<br />

custom class instead of dynamically adding them to the LocalConnection instance. This approach is demonstrated in<br />

the following code:<br />

package<br />

{<br />

import flash.net.LocalConnection;<br />

public class CustomLocalConnection ext<strong>en</strong>ds LocalConnection<br />

{<br />

public function CustomLocalConnection(connectionName:String)<br />

{<br />

try<br />

{<br />

connect(connectionName);<br />

}<br />

catch (error:Argum<strong>en</strong>tError)<br />

{<br />

// server already created/connected<br />

}<br />

}<br />

public function onMethod(timeString:String):void<br />

{<br />

trace("onMethod called at: " + timeString);<br />

}<br />

}<br />

}<br />

In order to create a new instance of the CustomLocalConnection class, you can use the following code:<br />

var serverLC:CustomLocalConnection;<br />

serverLC = new CustomLocalConnection("serverName");<br />

The second way to add callback methods is to use the LocalConnection.cli<strong>en</strong>t property. This involves creating a<br />

custom class and assigning a new instance to the cli<strong>en</strong>t property, as the following code shows:<br />

var lc:LocalConnection = new LocalConnection();<br />

lc.cli<strong>en</strong>t = new CustomCli<strong>en</strong>t();<br />

The LocalConnection.cli<strong>en</strong>t property indicates the object callback methods that should be invoked. In the<br />

previous code, the cli<strong>en</strong>t property was set to a new instance of a custom class, CustomCli<strong>en</strong>t. The default value for<br />

the cli<strong>en</strong>t property is the curr<strong>en</strong>t LocalConnection instance. You can use the cli<strong>en</strong>t property if you have two data<br />

handlers that have the same set of methods but act differ<strong>en</strong>tly—for example, in an application where a button in one<br />

window toggles the view in a second window.<br />

To create the CustomCli<strong>en</strong>t class, you could use the following code:<br />

package<br />

{<br />

public class CustomCli<strong>en</strong>t ext<strong>en</strong>ds Object<br />

{<br />

public function onMethod(timeString:String):void<br />

{<br />

trace("onMethod called at: " + timeString);<br />

}<br />

}<br />

}<br />

The third way to add callback methods, creating a dynamic class and dynamically attaching the methods, is very<br />

similar to using the LocalConnection class in earlier versions of ActionScript, as the following code shows:<br />

Last updated 6/6/2012<br />

830

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!