Actionscript 3 trace instance name

Thursday, December 23, 2010 at 1:35 PM
While I was working on a Flash project with AS 3.0 involved, I happen to deal with a weird problem when I want to get the instance name by using code. It always ends in names like "instance11" or "instance61". At last, I found out the solution to this problem. Here it is:



 Assuming I have a button named "btnTest" on stage and I want to get the instance name . Normally we usually code like this:


btnTest.addEventListener(MouseEvent.CLICK, clkTest);

private function clkTest(e:MouseEvent):void {

trace(e.target.name) // It'd output something random like "instance61" instead of the instance name of the MC you are targeting,
}

you should change a little bit, just 1 line of code, like this:
e.currentTarget.name

Finally, I get the proper result.
trace(e.currentTarget.name) // It'd output "btnTest"

0 comments

Post a Comment