Monday, December 29, 2008

e.target.name not working - as3 gotcha

Frustrated,...
So if you have ever encountered this :

head1.addEventListener(MouseEvent.CLICK, headClick);

private function headClick(e:MouseEvent):void {

trace(e.target.name)
}
// outputs something random like "instance22" instead of the instance name of the MC you are targeting, which in this case is "head1".

So after some searching and anger, (I still don't know why but this is only a random occurrence for me), if you add currentTarget in place of target

like so:
e.currentTarget.name

I get the proper results.
trace(e.currentTarget.name) // outputs "head1"; woohoo!

so if you keep seeing instance22, or instance286 instead of the instance name your looking for,. try the above.
if it still doesn't work, then something else is wrong,...

;}

19 comments:

Anonymous said...

Thanks for that, I've been stuck for like 2 hours on this. :)

Rex said...

Oops.. I have spent almost 3hours to trace the error... Thank you for that!!

Unknown said...

Thank you so much, you've just saved my life :D

shane michael colella said...

no prob. glad i could save some people some time!

Mike said...

Hey, thanks for this! I was really struggling with this.

Joey said...

Um... THANK YOU!

Charbel AC said...

Thank you, Thank you, Thank you :D
I spent a weekend struggling...

Anonymous said...

Thank you I have wasted more time searching for the answer to that than I want to admit.

ShyRain said...

Çok teşekkürler saatlerdir bunu araştırıyordum.

I mean: Thanks for that, ı have spent many time for finding this :))

Anonymous said...

awesome, thanks man, did a google search and found the answer i was lookin for right away.

Parag Gupta said...

thanks a lot.

Also try this:

e.target.parent.name;

e.target.name; returns the name of the instance of movie clip which is clicked. deep down.
for example I usually make a hit layer for all my movie clips which I want to use as buttons, this is what it returns. if i don't give it an instance name, then it gives all MCs instance names, and that's what you see.

Hope this helps.

Anonymous said...

OMG, thank you sooo much for this! you saved me so much time trying to figure this out. Damn AS3! ;)

Anonymous said...

thankyou so much!!! I think I already knew this but forgot it, so it was great to find this page :)

AKHIL said...

Any one help me. iwant to get the targetpath likeobject(root).movieclip1.movieclip2... which method is used to fin out this...

TALAL said...

Thank you so much. i did not know how to search this situation. but finally i reached at your blog thank you again for resolve this problem i was stuck with this situation hole day..

Anonymous said...

The reason you are seing the wrong name is because of event bubbling. When you click on a movie clip, the event is sent down through your display list hierarchy to the lowest level then returns back up to the source. So when you call event.target.name, you will be returning the deepest child's name, which could be anything on the stage or instantiated through code.

So, using e.currentTarget returns the object that first receives the event. You could also set the targets mouseChildren property to false.

Anonymous said...

Thank You!

sneddenblogger said...

Thank you,was stuck,and I had just started to hate flash.

DigitsCrawler said...

thank you!!