To use the Delegate class to listen for events:
1.
Create a new Flash document and save it as delegate.fla.
2.
Drag a Button component from the User Interface folder of the Components panel to
the library.
You add and position the button instance on the Stage using ActionScript in a later step.
3.
Add the following ActionScript to Frame 1 of the main Timeline:
import mx.controls.Button;
import mx.utils.Delegate;
function clickHandler(eventObj:Object):Void {
trace("[" + eventObj.type + "] event on " + eventObj.target + "
instance.");
trace("\t this -> " + this);
}
var buttonListener:Object = new Object();
buttonListener.click = function(eventObj:Object):Void {
trace("[" + eventObj.type + "] event on " + eventObj.target + "
instance.");
trace("\t this -> " + this);
};
this.createClassObject(Button, "one_button", 10, {label:"One"});
one_button.move(10, 10);
one_button.addEventListener("click", clickHandler);
this.createClassObject(Button, "two_button", 20, {label:"Two"});
two_button.move(120, 10);
two_button.addEventListener("click", buttonListener);
this.createClassObject(Button, "three_button", 30, {label:"Three"});
three_button.move(230, 10);
three_button.addEventListener("click", Delegate.create(this,
clickHandler));
The preceding code is separated into six sections (each section is separated by a blank
line). The first section imports the Button class (for the Button component) as well as the
Delegate class. The second section of code defines a function that you call when the user
clicks some of the buttons. The third section of code creates an object that you use as an
event listener, and the object listens for a single event,
348
Handling Events
.
click
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?