Stuff I am building:


Ted Patrick - Events & Community @ Adobe Systems


Note: This is the personal blog of Ted Patrick. The opinions and statements voiced here are my own.



mouseChildren=false

DIGG IT!     7 Comments Published Tuesday, December 30, 2008 at 8:35 AM .

There is a beautiful property called mouseChildren that I had completely forgotten about. mouseChildren=false turns off all mouse events at a node in the DisplayList so that event on children only reach the actual object where mouseChildren is set to false. Essentially this allows you to enable certain graphics/objects to be invisible to the DOM Event phases and behave as part of the object they are parented to.

Why would this be important?

Say you are writing a game or component and you make these amazing skins for all the elements in the game. If you do not set mouseChildren to false (default is true), you will get events within the skin and need to detect what Object you are working with when events occur. You will see code start to appear that reads:

//within event handler
if( event.target is Card ) do this...
//then the madness begins as you hunt up the DisplayList for the object in question
event.target.parent blah
event.target.parent.parent blah blah

By setting mouseChildren to false, events on any mouseChildren are given to the parent automagically.

ok back to coding...

Ted :)

7 Responses to “mouseChildren=false”

  1. # Anonymous Savvas Malamas

    I wish I didn't forgot that one all the time..
    It is an really useful property.. Not sure if I would love to have it as false by default but I really wish I could remember it often.. No that it exists on Ted's blog we might have one more reason to remember it.  

  2. # Anonymous mathis

    therefore I use event.currentTarget  

  3. # Anonymous David Côté

    Maybe I'm wrong (and correct me if I am), but isn't there a property on the event called currentTarget that returns the component on which the listener was actually added instead of an object within this component? That would prevent from searching the component with the parent property... But the mouseChildren=false when skinning is clearly the best way to go anyway :)  

  4. # Blogger Ted Patrick

    event.currentTarget implies that the listener is subscribed at that point.

    In my case I have 52 MovieClips (Cards with 10 Shapes avg) in all various states within Piles for a game. Instead of adding listeners directly to the Cards themselves, I implemented the game logic globally and thus there are few if any events directly subscribed to cards.

    The thing is that event listeners are expensive and the more you have the more you are forcing the player to do. By moving events above the Cards I have 150 less listeners.

    At the end of the day I would rather manage 6 things well than manage 150 things just to get the target right.

    In this case mouseChildren=false eliminated about 520 MovieClips from emitting events, now only the parent cards react.

    It is too bad that:
    stage.getObjectsUnderPoint( new Point( stage.mouseX , stage.mouseY ) ) ignores mouseChildren. This method returns all surfaces below the cursor regardless of type or mouseChildren value.

    Ted :)  

  5. # Blogger Ted Patrick

    mouseEnabled is the evil twin of mouseChildren. It enables you to turn off mouse events on individual items vs the all of mouseChildren.

    Ted :)  

  6. # OpenID Andy

    *thinking aloud* reglib for flash  

  7. # Blogger Nate Chatellier

    And furthermore, there is a fairly significant speed improvement:
    http://natejc.com/blog/?p=83  

Post a Comment

Where to find me:

Ted on Twitter - @AdobeTed
Ted on Adobe Groups
Ted on LinkedIn
Ted on Facebook
Ted at Adobe


Latest

Lists

Links

Jobs

Flex Jobs
city, state, zip

Archives