Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"this" referencing in AS3 1

Status
Not open for further replies.

firegambler

Technical User
Sep 25, 2002
455
0
0
AT
Hi Folks

I have a question on referencing objects:

In AS1 + AS2 when I used "this" within a function I was able to control the object calling the function.
Now in AS3 it seems as if this would always target the timeline on which the function is defined.
How can I make sure to reference the calling object without using the whole absolute path to it?

Example:
Earlier in AS2 I had a button, named button1.
Code written on the main timeline. Pressing the button moved the button.
Code:
function moveright(){
	this._x=this._x+5;
	}
button1.onRelease=function(){
moveright();
}

Now in AS3, code - again - is written on the main timeline.
Pressing the button moves the whole film.
Code:
function MoveRight(event:MouseEvent):void 
{
	this.x+=5;
	}
MC_button2.addEventListener(MouseEvent.MOUSE_DOWN, MoveRight)

How can I achieve what I did in AS2 with AS3?

Thanks in advance
 
Either:
Code:
function MoveRight(event:MouseEvent):void
{
   event.target.x+=5;
}
MC_button2.addEventListener(MouseEvent.MOUSE_DOWN, MoveRight);
or
Code:
function MoveRight(event:MouseEvent):void
{
   MC_button2.x+=5;
}
MC_button2.addEventListener(MouseEvent.MOUSE_DOWN, MoveRight);
...and do not worry, soon you'll find yourself hating old AS1/2 ways :)

Kenneth Kawamoto
 
Hi Kenneth,

thanks for helping me out!

In fact I am really having a hard time getting into AS3 but I hope your encouraging comment proves true :)

Cheers

f.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top