Skip to main content
Known Participant
November 25, 2008
Answered

"this" in as3

  • November 25, 2008
  • 2 replies
  • 507 views
How would I do this in AS3? I want to call a function and run it with specific properties depending on which button is clicked. So how do the "_this" statement work in as3? in as2 I would write something like this and "this" would stand for the instance name:
This topic has been closed for replies.
Correct answer Newsgroup_User
You just call the same function when you set up your event listener - and
then use the target property of the event to get which button was clicked.
For instance, say you have two buttons on stage - instance names a and b:

a.addEventListener(MouseEvent.CLICK, btnPressed);
b.addEventListener(MouseEvent.CLICK, btnPressed);

function btnPressed(e:MouseEvent){
trace(e.target.name);
e.target.y -= 10;
}


--
Dave -
www.offroadfire.com
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/


2 replies

kglad
Community Expert
Community Expert
November 25, 2008
"this" works the same in as3 as it does in as2.
Newsgroup_UserCorrect answer
Inspiring
November 25, 2008
You just call the same function when you set up your event listener - and
then use the target property of the event to get which button was clicked.
For instance, say you have two buttons on stage - instance names a and b:

a.addEventListener(MouseEvent.CLICK, btnPressed);
b.addEventListener(MouseEvent.CLICK, btnPressed);

function btnPressed(e:MouseEvent){
trace(e.target.name);
e.target.y -= 10;
}


--
Dave -
www.offroadfire.com
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/