MACROMEDIA FLASH MX 2004-USING COMPONENTS Use Manual page 65

Using components
Hide thumbs Also See for FLASH MX 2004-USING COMPONENTS:
Table of Contents

Advertisement

Delegating the scope of a function
The
addEventListener()
to a listener. The listener can either be an object or a function. If you pass an object, the callback
function assigned to the object is invoked in the scope of the object. However, if you pass a
function, the function is invoked in the scope of the component instance that calls
addEventListener()
Because the function is invoked in the scope of the broadcaster instance, the keyword
body of the function points to the broadcaster instance, not to the class that contains the
function. Therefore, you cannot access the properties and methods of the class that contains the
function. Use the Delegate class to delegate the scope of a function to the containing class so that
you can access the properties and methods of the containing class.
The following example uses the same approach as the previous section with a variation of the
Cart.as class file:
import mx.controls.Button;
import mx.controls.CheckBox;
class Cart {
var myCheckBox_chb:CheckBox;
var myButton_btn:Button;
// define a variable to access
// from the chb_onClick function
var i:Number = 10
function onLoad() {
myCheckBox_chb.addEventListener("click", chb_onClick);
}
function chb_onClick(eventObj:Object) {
// You would expect to be able to access
// the i variable and output 10.
// However, this sends undefined
// to the Output panel because
// the function isn't scoped to
// the Cart instance where i is defined.
trace(i);
}
}
To access the properties and methods of the Cart class, call
parameter of
addEventListener()
import mx.utils.Delegate;
import mx.controls.Button;
import mx.controls.CheckBox;
class Cart {
var myCheckBox_chb:CheckBox;
var myButton_btn:Button;
// define a variable to access
method requires two parameters: the name of an event and a reference
. (For more information, see
, as follows:
"About scope in listeners" on page
Delegate.create()
61.)
in the
this
as the second
Delegating events
65

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH MX 2004-USING COMPONENTS and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Flash mx

Table of Contents