4

Happy new year btw !

I want to separate event handling from a container and it child. So as you can see, my source code is very simple :

    package  {
    import flash.display.Sprite;
    import flash.display.*;
    import flash.events.*;

    public class test extends Sprite{

        public function test() {
            var container:Sprite = new Sprite();  // my container
            container.graphics.beginFill(0, 1);  // whatever the color
            container.graphics.drawRect(0, 0, 100, 100); // origin at 0,0
            container.graphics.endFill();
            addChild(container);

            var decor:Sprite = new Sprite();  // and it child
            decor.graphics.beginFill(0, 1);  // whatever the color
            decor.graphics.drawRect(200, 200, 100, 100);  // origin at 200,200
            decor.graphics.endFill();
            container.addChild(decor);
            container.mouseChildren = false;
            container.addEventListener(MouseEvent.ROLL_OVER, onOver, false, 0, true);
        }
        private function onOver(e: MouseEvent):void {
            trace("ROLL trace");
        }
    }
}

When I roll over the container object, I've got the trace (OK for me). BUT When I roll over the decor object, I've got the trace too (not what I want). I just want the container to be triggered by the mouse event, not it child. So what's happened to my mouseChildren = false....? I don't understand...

CodeMouse92
  • 6,840
  • 14
  • 73
  • 130
nouatzi
  • 735
  • 4
  • 14

2 Answers2

4

The decor object is a member of container, and therefore it is evaluated along with any other content within container.

mouseChildren = false; is not a way to completely disable mouse events, but to reduce complexity within composite display objects: A mouse event is still fired, but the event's target property will not contain a reference to the child object the mouse actually rolled over, but only to the parent that the property was set on.

If you want decorto be completely ignored, use decor.mouseEnabled = false;instead.

weltraumpirat
  • 22,544
  • 5
  • 40
  • 54
  • +1 right on. Just some complimentary info for @nouatzi, from the language reference http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#mouseChildren. Always check the AS3 docs! They're full of great nfo. –  Jan 03 '12 at 01:46
1

I've tried mouseEnabled = false, and it's not working either. In another forum, a guys told me that 'a filled object within container will trigger the event handler'. So his solution is to have the container, and create 2 children : one handling the mouse event, and the other one as decor.

And it's working pretty well.

nouatzi
  • 735
  • 4
  • 14