Like it or lump it

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.

Previous

Wasted potential is a blog!

Next

Flash AS3: Sequential loading with BulkLoader

28 Comments

  1. gamez

    very good tutotrial…good job 🙂

  2. mrProper

    Great tut!!!

  3. Savargas

    Excellent, Thank you

  4. Patrick

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

  6. gLiheng

    nice, this explains my problem, thank u !

  7. creata

    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

  8. admin

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

  9. creata

    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

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

  11. Great example! Thanks!

  12. Kirubakar

    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

  13. Bob

    Awesome, thanks!

  14. ian

    nice tutorial, helps me clear on that!!

  15. Rajib Datta

    Nice

  16. Thanks, realy helpful stuff.

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

  18. Piotr

    Thanks, it cleared up things to me.

  19. Thanks for the great, useful tutorial. Well done.

  20. alile

    excellent workup.

  21. Great example. You are best.

  22. Good Example used for this Tutorial

  23. suman

    thankS,gooD examplE

  24. Christopher Gan

    Very good job. Thank you.

  25. Takan

    Thank you so much.

  26. Rajay

    super…..! its really helpful ..Thanks

Powered by WordPress & Theme by Anders Norén