In the Actions panel, enter the following code:
8.
var myButton:mx.controls.Button;
var myText:mx.controls.TextInput;
function click(evt){
myText.text = evt.target;
}
myButton.addEventListener("click", this);
The
property of the event object,
target
event. This code displays the value of the
To register a listener object in a class (AS) file:
Open the file TipCalculator.fla from the location specified in
1.
on page
43.
Open the file TipCalculator.as from the location specified in
2.
on page
43.
In the FLA file, select form1 and view the class name, TipCalculator, in the Property inspector.
3.
This is the link between the form and the class file. All the code for this application is in the file
TipCalculator.as. The form assumes the properties and behaviors defined by the class assigned
to it.
In the AS file, scroll to line 25,
4.
The
onLoad()
function, the
percentRadio15
method to register a listener with an event.
Look at line 27,
5.
When you call
indicating the name of the event that is broadcast—in this case,
reference to either an object or a function that handles the event. In this case, the parameter is
the keyword
this
the object for a function with the name of the event.
Look at line 63,
6.
This is the function that executes when the
Select the TipCalculator.fla and select Control > Test Movie to test the file.
7.
Using the handleEvent callback function
You can also use listener objects that support a
the event that is broadcast, the listener object's
or a
if else
switch
an
statement to handle the
if else
// define the handleEvent function
// pass it evt as the event object parameter
public function onLoad():Void
function executes when the form loads into Flash Player. In the body of the
TextInput instance and the three RadioButton instances,
subtotal
,
percentRadio18
subtotal.addEventListener("change", this)
addEventListener()
, which refers to an instance of the class file (an object). Flash then looks on
public function change(event:Object):Void
statement to handle multiple events. For example, the following code uses
click
, is a reference to the instance broadcasting the
evt
property in the TextInput component.
target
, and
percentRadio20
, you must pass it two parameters. The first is a string
TextInput instance changes.
subtotal
handleEvent
handleEvent
and
events:
change
"Working with Components"
"Working with Components"
.
, call the
addEventListener()
.
. The second is a
"change"
.
function. Regardless of the name of
method is called. You must use an
Using listeners to handle events
59
Need help?
Do you have a question about the FLASH MX 2004-USING COMPONENTS and is the answer not in the manual?