moock.org is supported in part by


July 16, 2003

reader mail: accessing Button._name from an on() handler

sanjay asks:
How do i reference the name of a button (p 463 of ASDG2e) from an on() handler.

Can't get it to work...

===================
colin answers:
===================
hi sanjay,
if you use on() handlers, you don't have an easy way to get at the Button class's _name property. to access _name you need a reference to the button object to which the on() handler is attached. however, from an on() handler attached to a button, the keyword 'this' refers the the movieclip on whose timeline the button resides. that's not what you want. from inside the on() handler, you could refer to the button object as this.yourButtonName, but i'm guessing that's not possible because you're trying to get the button's name dynamically.

so, if you need access to the button when it's clicked, try assigning the button event handler as a property rather than with "on()", as in:

theButton.onRelease = function {
// do stuff here...

// in this style of event handler, the following
// statement will display the name of the button
// in the output window
trace(this._name);
}

you could also use an on() handler attached to a movie clip. in that case, the keyword 'this' refers to the clip with the on() handler, so you would be able to use 'this._name'. for info on using movie clips as buttons, see page 364 of asdg2.

for much more info on on() handlers, see the section on event handler scope for on(). p234-p237 of asdg2. see also the table describing the values of the 'this' keyword in event handlers.

colin

Posted by moock at July 16, 2003 01:19 PM
Comments