Flash AS3: target vs. currentTarget
While learning Actionscript 3, my button rollovers were not working correctly. The problem was that I didn’t know the difference between target and currentTarget or between MOUSE_OVER and ROLL_OVER. I made an example to demonstrate and I hope to explain it in a way that will save you some time.
AS3 Mouse events use two different properties: target and currentTarget. “target” is the object that fired the MOUSE_OVER (or MOUSE_OUT) event. “currentTarget” is the Movieclip that you applied the listener to. In the example below, the top 2 buttons have MOUSE_OVER and MOUSE_OUT listeners. Each button is made up of 6 different MovieClips. As you rollover the button on the left, the target fades. As you rollover the button on the right, the currentTarget fades. So, you want to use currentTarget right?
WRONG! Watch the output text window at the bottom of the Flash movie as you rollover the button on the right. Even though it appears to be highlighting correctly, it is actually firing a MOUSE_OVER event for every movieclip inside the button! This is not what you want! You want the event to fire once – when you rollover the button. This is where mouseChildren comes in! In the second row of buttons, I have set the mouseChildren property of both buttons to false. This means that none of the internal movieclips will fire an event. Now, when you rollover the buttons, you can see that they highlight correctly and that the target and currentTarget values are the same.
But there is an easier way – use ROLL_OVER and ROLL_OUT! These events automatically ignore the the internal structure of the buttons and register events only on the object that the listener was assigned to (in this case, the button itself). Most of the time, these are the events you will want to use. I rarely use MOUSE_OVER or MOUSE_OUT for anything.
I hope this example helps to clear up some confusion. You can download the source files here and play around with them.
very good tutotrial…good job
Great tut!!!
Excellent, Thank you
Thank you! I’ve been trying to figure out what the hell was going on with my flash app, but you’ve really cleared it up for me. Thanks again!
Great Example!
Fan-f’ing-tastic! I spent about four hours trying to figure out why my MOUSE_OVER event was firing an immediate MOUSE_OUT…. it was the children in the mc!
However, maybe I did something else different, but I found that I also needed the “mouseChildren” set to FALSE even when I used ROLL_OVER and ROLL_OUT… weird.
Once again, the internet has saved my life.
nice, this explains my problem, thank u !
Here what I wanted ask you:
If we use CLICK instead of ROLL, how can we fire an independent event from the differents elemenst inside the MovieClip? e.g. I have a menu created dynamicly where the elements have a submenu inside. How is possible to fire events from the main element and separatly from the elements of the submenu?
target or currentTarget or other way.
RO
@creata
You want to use target to retrieve the internal MovieClip element events, but you may find the results unpredictable – you may not get the target you are expecting. In this case, you will have to set the mouseChildren and mouseEnabled properties of the elements you don’t want to false. An easier way to do it is to make each submenu element into a movieclip and add a listener to each one individually. That way, you can always be sure that you are receiving only the specific events that you want.
Thanks for your answer and I’m going to change the logic of my project to create the elements and subelements. I’m trying to develope a elastic dinamic (XML) menu full animated by TweenLite. I’ll show you the result at the end if you want to.
Thanks again for your help.
RO
Regarding adding listener to each sub menu item in a movie clip isnt good when you have too many of of them and if they are set into the time line of the movie clip, Its even more of a bad idea. it would be good to use target.name property and validate for the required sub menu items.
Thanks
Great example! Thanks!
Hei Machi Super da,
Really excellent work, i m confused of this Events but now i m clear
Thanx machi, carry on ur great work, my best wishes
Awesome, thanks!
nice tutorial, helps me clear on that!!
Nice
Thanks, realy helpful stuff.