User Guide

1130 SimpleButton class
The second usage example uses an on() handler and must be attached directly to a Button
component instance. The keyword
this, used inside an on() handler attached to a
component, refers to the component instance. For example, the following code, attached to
the Button component instance
myButtonComponent, sends “_level0.myButtonComponent”
to the Output panel:
on (click) {
trace(this);
}
The behavior of this is different when used inside an on() handler that is attached to a
regular Flash button symbol; in this instance,
this refers to the that contains the button. For
example, the following code, attached to the button symbol instance
myButton, sends
“_level0” to the Output panel:
on (release) {
trace(this);
}
Example
This example, written on a frame of the timeline, sends a message to the Output panel when a
button called
buttonInstance is clicked. The first line specifies that the button act like a
toggle switch. The second line creates a listener object called
form. The third line defines a
function for the
click event on the listener object. Inside the function is a trace() statement
that uses the event object that is automatically passed to the function (in this example,
eventObj) to generate a message. The target property of an event object is the component
that generated the event (in this example,
buttonInstance). The SimpleButton.selected
property is accessed from the event objects
target property. The last line calls
addEventListener() from buttonInstance and passes it the click event and the form
listener object as parameters.
buttonInstance.toggle = true;
var form:Object = new Object();
form.click = function(eventObj:Object) {
trace("The selected property has changed to " +
eventObj.target.selected);
};
buttonInstance.addEventListener("click", form);
NOTE
The built-in ActionScript Button object doesn’t have a click event; the closest event
is
release.