Using button and movie clip event handlers 337
Instead of using a listener object, as in the first procedure under “Using event listeners with
components” on page 335, you can use a custom function. The code in the previous example
could be rewritten as follows:
System.security.allowDomain("http://www.helpexamples.com");
my_ldr.addEventListener("progress", progressListener);
my_ldr.addEventListener("complete", completeListener);
my_ldr.load("http://www.helpexamples.com/flash/images/image1.png");
function progressListener(evt_obj:Object):Void {
trace(evt_obj.type); // progress
trace("\t" + evt_obj.target.bytesLoaded + " of " +
evt_obj.target.bytesTotal + " bytes loaded");
}
function completeListener(evt_obj:Object):Void {
trace(evt_obj.type); // complete
}
Using button and movie clip event
handlers
You can attach event handlers directly to a button or movie clip instance on the Stage by using
the
onClipEvent() and on() event handlers. The onClipEvent() event handler broadcasts
movie clip events, and the
on() event handler handles button events.
To attach an event handler to a button or movie clip instance, click the button or movie clip
instance on the Stage to bring it in focus, and then enter code in the Actions panel. The title
of the Actions panel reflects that code will be attached to the button or movie clip: Actions
Panel - Button or Actions Panel - Movie Clip. For guidelines about using code that’s attached
to button or movie clip instances, see “Attaching code to objects” on page 746.
NOTE
In the previous examples, the event listeners are always added before the
Loader.load() method is called. If you call the Loader.load() method before you
specify the event listeners, the load might complete before the event listeners are
fully defined. This means that the content might display and the
complete event might
not be caught.
NOTE
Do not confuse button and movie clip event handlers with component events, such as
SimpleButton.click, UIObject.hide, and UIObject.reveal, which must be
attached to component instances and are discussed in Using Components.