Home > Actionscript/AS3, Flash > Flash AS3: target vs. currentTarget

Flash AS3: target vs. currentTarget

April 13th, 2009

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.

Be Sociable, Share!

  1. gamez
    May 14th, 2009 at 11:24 | #1

    very good tutotrial…good job :)

  2. mrProper
    June 17th, 2009 at 05:36 | #2

    Great tut!!!

  3. Savargas
    July 6th, 2009 at 19:55 | #3

    Excellent, Thank you

  4. Patrick
    August 10th, 2009 at 12:58 | #4

    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!

  5. August 11th, 2009 at 05:57 | #5

    Great Example!

  6. October 24th, 2009 at 12:57 | #6

    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.

  7. gLiheng
    October 29th, 2009 at 03:59 | #7

    nice, this explains my problem, thank u !

  8. creata
    November 15th, 2009 at 09:35 | #8

    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

  9. admin
    November 16th, 2009 at 08:21 | #9

    @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.

  10. creata
    November 16th, 2009 at 15:24 | #10

    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

  11. December 18th, 2009 at 07:14 | #11

    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.

  12. January 16th, 2010 at 09:27 | #12

    Thanks :)

  13. January 30th, 2010 at 05:09 | #13

    Great example! Thanks!

  14. Kirubakar
    February 1st, 2010 at 06:55 | #14

    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

  15. Bob
    March 27th, 2010 at 09:36 | #15

    Awesome, thanks!

  16. ian
    June 20th, 2010 at 12:08 | #16

    nice tutorial, helps me clear on that!!

  17. Rajib Datta
    June 26th, 2010 at 04:14 | #17

    Nice

  18. August 25th, 2010 at 01:35 | #18

    Thanks, realy helpful stuff.

  19. September 30th, 2010 at 13:17 | #19

    Thank you so much for this article…and the example is excellent!!! You saved me so much time.

  20. Piotr
    December 3rd, 2010 at 14:44 | #20

    Thanks, it cleared up things to me.

  21. June 6th, 2011 at 05:01 | #21

    Thanks for the great, useful tutorial. Well done.

  22. alile
    July 28th, 2011 at 10:22 | #22

    excellent workup.

  23. September 17th, 2011 at 20:50 | #23

    Great example. You are best.

  24. November 22nd, 2011 at 04:38 | #24

    Good Example used for this Tutorial

  25. suman
    December 22nd, 2011 at 02:11 | #25

    thankS,gooD examplE

  26. Christopher Gan
    February 3rd, 2012 at 00:34 | #26

    Very good job. Thank you.

  27. Takan
    August 19th, 2012 at 21:19 | #27

    Thank you so much.

  28. Rajay
    February 5th, 2013 at 07:26 | #28

    super…..! its really helpful ..Thanks

  1. October 14th, 2009 at 11:03 | #1
  2. January 23rd, 2010 at 06:44 | #2
  3. February 10th, 2010 at 14:10 | #3
  4. May 23rd, 2010 at 09:03 | #4
  5. August 24th, 2010 at 06:09 | #5

Top