Difference between MouseEvent.ROLL_OVER and MouseEvent.MOUSE_OVER in AS3

This was a question that has been in my head for a long time: what is the difference between MouseEvent.ROLL_OVER and MouseEvent.MOUSE_OVER. I have been using MouseEvent.MOUSE_OVER for all my rollOvers up to now without really thinking about it, but I think it would be interesting to know the difference for something so basic.

If you look at the documentation that comes with Flash CS3. You will find very little help. There are no examples explaining the difference between the two. The only difference you will find there is that the MOUSE_OVER event has the bubbles property set to true and the ROLL_OVER to false. I don’t know how much that property impacts on the behavior of the event, but I am keeping event bubbling for another post (I have been postponing that post for a long time).

Where I found there was a difference between the two events is in the way they handle children of the display object you added the listener too. I have made a simple example to show this:

In that flash example, be sure to roll over the blue part of the button to really see the difference(note that the blue rectangle is inside the black rectangle movieClip, not just over). In the MOUSE_OVER example, the event is fired for every children of the display object that the listener was added to. This does not happen with the ROLL_OVER event. If you move your mouse from the exterior of the button to the blue part MOUSE_OVER will generate 3 times the events that ROLL_OVER will. One way to make MOUSE_OVER act as ROLL_OVER is to set mouseChildren = false but as a consequences: the first is that it will disable all mouse events from children which I think might help the CPU but at the same time you won’t be able to interact with the children with the mouse.

In our previous example, it didn’t really matter how many events where fired because we used a tween to do the rollover and we weren’t able to see the difference visually, but there might be times where you will want all the different events. Like if you want the mouse cursor to change when over a movieClip and still want rollOver effects from children of that movieClip, you would use the MouseEvent.ROLL_OVER to change the mouse cursor and MouseEvent.MOUSE_OVER for the children movieClips rollOver effects.

As a side note on cpu efficiency, it doesn’t really matter if you listen to  MouseEvent.ROLL_OVER or MouseEvent.MOUSE_OVER events, they are both as efficient. What will save you cpu cycles is if you disable the children of a movieClip from sending events if you don’t need them (even if you don’t listen to them events are still fired) using mouseChildren =  false.

I hope this shed some light on the subject.

, , , ,

  1. #1 by ask - June 24th, 2008 at 11:20

    Yep, you just saved me a few hours work for sure. Clear explanation and nice examples. Cheers very much. And good look with the event bubbling post. Ask.

  2. #2 by Janosch - June 30th, 2008 at 10:17

    thanks!

  3. #3 by Gary - June 30th, 2008 at 18:30

    Nice! thanks for the explanation, now send it to Adobe for their, seriously lacking, docs.

  4. #4 by Ronald - July 29th, 2008 at 03:17

    Ok cool, I was wondering about this.

    btw, lol @ gary

  5. #5 by Bruce - August 14th, 2008 at 09:06

    Agrrr, finally somebody explains this. Thanks you saved the day.

  6. #6 by TePig - September 17th, 2008 at 22:28

    It’s a cool and great example !

  7. #7 by Paul - September 30th, 2008 at 07:01

    If only I’d found this post earlier, I wouldn’t have spent the morning banging my head trying to fix an issue related to using MouseEvent.MOUSE_OUT – I was even doing weird stuff with getRect( this ).containsPoint( ) etc. to try and get around it!

    Changed it all to ROLL_OVER/ROLL_OUT and it works first time!

    Anyway, thanks for the post and examples. You may have just saved my productivity for today ;)

  8. #8 by Howie - October 22nd, 2008 at 09:57

    Great example. I was in the same boat as Paul and headed down that same path. Whew! You just saved me. Thanks.

  9. #9 by kj - November 12th, 2008 at 11:51

    brilliant!

    that helps alot!!

    :)

  10. #10 by Candy - December 10th, 2008 at 01:27

    great help, thanks.

  11. #11 by joan.mmk - January 1st, 2009 at 22:35

    Nice! I’ve got the same question. Thanks.

  12. #12 by cakeasaurus - January 11th, 2009 at 19:20

    good explanation. i was having this problem for a while, i had no idea why the mouse_over and mouse_out were firing twice.. turned out it was happening for each of the children display objects.

  13. #13 by J - February 8th, 2009 at 12:29

    these nifty little helpers.. thanks a lot for sharing!

  14. #14 by Tom - February 17th, 2009 at 18:08

    THANKS so much for this explanation, right now I am working on a project where I need extactly this!!!

    THANX AGAIN!

  15. #15 by Bill - March 17th, 2009 at 14:03

    You saved me a day of work with this. Thank you, thank you, thank you!

  16. #16 by Chris - March 26th, 2009 at 06:28

    I’m having a possibly related issue… I’ve got an event listener on a container for MouseEvent.ROLL_OVER… the container has several children, some of which I have set mouseEnabled = false, because I don’t want the rollover to pick them up… but it still sees them as part of the main container and fires off the event whenever I roll over them. Any thoughts?

  17. #17 by zedia.net - March 27th, 2009 at 16:49

    @Chris
    Ya I was having a similar issue this week. The trouble is that even if a children is set to mouseEnabled = false, it still counts in the boundaries of the parent; it will just not send events…

    You will have to find another way around this.Mostly by putting these troublesome children in another holder DisplayObject with mouseEnabled= false also.

  18. #18 by Matt - April 16th, 2009 at 03:14

    @Chris
    Or you could make use of the hitArea property of the MovieClip/Sprite, by not putting the troublesome children into the clip you use for the hitArea

  19. #19 by Ian - April 20th, 2009 at 14:44

    Great demonstration! I wonder why Adobe didn’t see fit to clarify this in the documentation?

  20. #20 by Simon - April 23rd, 2009 at 15:23

    THANK YOU SO MUCH! you just saved me so much trouble.

  21. #21 by AMITKUMARDUBEY - June 3rd, 2009 at 04:49

    Really great explanation………….
    With proper example.

    Please send this to adobe for their correction in Explanation.

  22. #22 by pez - July 2nd, 2009 at 18:03

    awesome man i waved the mouse over the examples a few times and instantly understood what people had explained in much less clear terms on forums

(will not be published)
Subscribe to comments feed