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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Using the external API<br />

Exposing ActionScript methods to JavaScript<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

As the previous example showed, once the code determines that the browser is ready, the setupCallbacks() method<br />

is called. This method prepares ActionScript to receive calls from JavaScript, as shown here:<br />

private function setupCallbacks():void<br />

{<br />

// Register the SWF cli<strong>en</strong>t functions with the container<br />

ExternalInterface.addCallback("newMessage", newMessage);<br />

ExternalInterface.addCallback("getStatus", getStatus);<br />

// Notify the container that the SWF is ready to be called.<br />

ExternalInterface.call("setSWFIsReady");<br />

}<br />

The setCallBacks() method finishes the task of preparing for communication with the container by calling<br />

ExternalInterface.addCallback() to register the two methods that will be available to be called from JavaScript.<br />

In this code, the first parameter—the name by which the method is known to JavaScript ("newMessage" and<br />

"getStatus")—is the same as the method’s name in ActionScript. (In this case, there was no b<strong>en</strong>efit to using differ<strong>en</strong>t<br />

names, so the same name was reused for simplicity.) Finally, the ExternalInterface.call() method is used to call<br />

the JavaScript function setSWFIsReady(), which notifies the container that the ActionScript functions have be<strong>en</strong><br />

registered.<br />

Communication from ActionScript to the browser<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The Introvert IM application demonstrates a range of examples of calling JavaScript functions in the container page.<br />

In the simplest case (an example from the setupCallbacks() method), the JavaScript function setSWFIsReady() is<br />

called without passing any parameters or receiving a value in return:<br />

ExternalInterface.call("setSWFIsReady");<br />

In another example from the isContainerReady() method, ActionScript calls the isReady() function and receives<br />

a Boolean value in response:<br />

var result:Boolean = ExternalInterface.call("isReady");<br />

You can also pass parameters to JavaScript functions using the external API. For instance, consider the IMManager<br />

class’s s<strong>en</strong>dMessage() method, which is called wh<strong>en</strong> the user is s<strong>en</strong>ding a new message to his or her “conversation<br />

partner”:<br />

public function s<strong>en</strong>dMessage(message:String):void<br />

{<br />

ExternalInterface.call("newMessage", message);<br />

}<br />

Once again, ExternalInterface.call() is used to call the designated JavaScript function, notifying the browser of<br />

the new message. In addition, the message itself is passed as an additional parameter to ExternalInterface.call(),<br />

and consequ<strong>en</strong>tly it is passed as a parameter to the JavaScript function newMessage().<br />

Last updated 6/6/2012<br />

852

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

Saved successfully!

Ooh no, something went wrong!