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,...
;}
Monday, December 29, 2008
e.target.name not working - as3 gotcha
Posted by shane michael colella at 1:48 PM
Labels: as3, as3 e.target gotcha, currentTarget.name, e.currentTarget, e.target instance name, e.target.name, flash, instance name
Subscribe to:
Post Comments (Atom)
19 comments:
Thanks for that, I've been stuck for like 2 hours on this. :)
Oops.. I have spent almost 3hours to trace the error... Thank you for that!!
Thank you so much, you've just saved my life :D
no prob. glad i could save some people some time!
Hey, thanks for this! I was really struggling with this.
Um... THANK YOU!
Thank you, Thank you, Thank you :D
I spent a weekend struggling...
Thank you I have wasted more time searching for the answer to that than I want to admit.
Çok teşekkürler saatlerdir bunu araştırıyordum.
I mean: Thanks for that, ı have spent many time for finding this :))
awesome, thanks man, did a google search and found the answer i was lookin for right away.
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.
OMG, thank you sooo much for this! you saved me so much time trying to figure this out. Damn AS3! ;)
thankyou so much!!! I think I already knew this but forgot it, so it was great to find this page :)
Any one help me. iwant to get the targetpath likeobject(root).movieclip1.movieclip2... which method is used to fin out this...
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..
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.
Thank You!
Thank you,was stuck,and I had just started to hate flash.
thank you!!
Post a Comment