PDA

View Full Version : "this" reference with buttonInstance.onPress?



jantom
May 10th, 2004, 08:50 PM
I have a button on the main timeline, its instance name is "btButton".

1.

If I put:
"btButton.onPress function {this.attachMovie(..etc..);}"
nothing happens.

I need to refer to "this._parent". Why?
I thought that since the button was on the main timeline, "this" should refer to the main timeline.

Again:
"btButton.onPress function {this._alpha=10;}
changes the alpha property of the button, while
"btButton.onPress function {this._parent._alpha=10;}" changes the alpha property of the _root.


2.
"this._parent.attachMovie(...)" works.
"_parent.attachMovie(...)" doesn't work. Why?

Adam
May 10th, 2004, 08:54 PM
When you define your function with but.onPress....anything inside the function with "this" refers to the button itself, same as with a MC, like mcInstance.onRelease ... and if you have a this.play() the MC is going to play, not the main timeline,...make sense?

Adam

lostinbeta
May 10th, 2004, 10:29 PM
Dynamic event handlers are object oriented, so as Adam stated, using "this" inside the handler will refer to the button itself (where such is not so if you use a static on handler)

this._parent however refers to the timeline that contains "this".

nunomira
May 10th, 2004, 10:42 PM
another way to deal with the situation is using a variable (possibly a global variable) that points to the (main) timeline.


var here = this;
my_btn.onRelease = function()
{
here.attachMovie("...");
};

or


_global.here = this;
my_btn.onRelease = function()
{
here.attachMovie("...");
};