User Guide

196 Chapter 9: Using Flash, Flash Components, and Other Interactive Media Types
Setting up callbacks
The next step is to set up callback handlers with the
setCallback() method. You should set up a
callback for each event you expect the object to generate. Local connection objects generate
onStatus and allowDomain events, as well as an event for each incoming message received.
These incoming message events are named by the string that is passed as the subject, or first
parameter, of the message.
To set up each callback, use the
setCallback() method and include the name of the local
connection object, the name of the event to respond to, the script handler name, and the script
object that contains the handler as arguments with the method.
To set up localConnection callbacks:
1 Set up the onStatus callback. An onStatus event is generated each time a message is sent
by the local connection object and contains information about the success or failure of the
send operation.
sprite(name).setCallback(pLocalConn, "onStatus", #myOnStatus, me)
This script statement sets a callback for the event named onStatus generated by the object
pLocalConn. The script handler name is myOnStatus and the script object that contains it is
the same script object that contains this
setCallback() method, referred to as me. The quotes
around the event name and the pound sign (#) preceding the handler name. The pound sign
converts the handler name to a symbol.
The actual callback handlers, such as the
myOnStatus handler, are illustrated later in
this section.
2 Set up the allowDomain callback. An allowDomain event is generated each time the local
connection object receives an incoming message.
sprite(name).setCallback(pLocalConn, "allowDomain", #myAllowDomain, me)
In this case, the event name is AllowDomain and the handler name is myAllowDomain.
If the
myAllowDomain handler determines that the message is coming from an allowed
domain, an event named with the message subject is generated. When a movie is running
from a user’s computer and not in a browser, the domain is
localHost. When the movie is
running in a browser, the domain is determined by the server that hosts the movie, such as
macromedia.com.
3 Set up the callback for these user-defined messages. Decide what string you want to use as your
message subjects, so that you know what the event name will be. In the following example, the
user-defined message subject and event name are
incomingMessage:
sprite(name).setCallback(pLocalConn, "incomingMessage", #myIncomingMessage,
me)
Writing callback handlers
Now the callbacks are set up. In order for them to work, however, you must write the callback
handlers themselves. While the
setCallback() methods are all inside a beginSprite handler in
this example, the callback handlers must be outside the
beginSprite handler because they are
handlers by themselves. In this example, the callback handlers are located in the same script
attached to the Flash sprite, just after the
beginSprite handler.