Question

I have a series of MovieClips, held within several MovieClips, that dispatch a custom move event that I would like the stage to listen for and interact with. Is there a way to add to the stage (or any Object) an event listener for any event of one type, regardless of where it was dispatched from?

For instance, could I add to the stage an event listener to listen for any EVENT.COMPLETE that was dispatched from a child, or any of its children, or any of it's children's children?

Was it helpful?

Solution

What you are looking for is event bubbling. Set the third property of a new event to true to enable bubbling, and it will bubble up the display list, starting from the object dispatching the event to the stage. See the official docs for more info.

Tyler.

Edited: Change "starting from the stage" to "object dispatching the event"

OTHER TIPS

Well, this is something I'll never understand... Why people always use bubbling events to capture events on the stage?

If you look at the doc, event is propagated from the stage to the target object (capture phase) and then bubbles if you enable bubbling.

So ... just use capture :

Main.as

package {
    import flash.display.Sprite;

    public class Main extends Sprite
    {
        public function Main()
        {
            var c:Circle = new Circle();
            var r:Rect = new Rect();
            c.addChild(r);
            addChild(c);
            addEventListener(CustomEvent.CUSTOM, customEventHandler, true);//don't forget third parameter to use capture
        }

        private function customEventHandler(e:CustomEvent):void
        {
            trace(e.eventPhase == EventPhase.CAPTURING_PHASE);//shows true
        }
    }
}

Circle.as

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Circle extends Sprite
    {
        public function Circle()
        {
            super();
            init();
        }

        private function init():void
        {
            with(graphics)
            {
                beginFill(0xFF0000);
                drawCircle(25, 25, 50);
                endFill()
            }

        }
    }
}

Rect.as

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

    public class Rect extends Sprite
    {
        public function Rect()
        {
            super();
            init();
        }

        private function init():void
        {
            with(graphics)
            {
                beginFill(0x000000);
                drawRect(0, 0, 25, 25);
                endFill();
            }

            addEventListener(MouseEvent.CLICK, mouseClickHandler);
        }

        private function mouseClickHandler(e:MouseEvent):void
        {
            dispatchEvent(new CustomEvent(CustomEvent.CUSTOM));
        }


    }
}

CustomEvent.as

package
{
    import flash.events.Event;

    public class CustomEvent extends Event
    {
        public static const CUSTOM:String = "custom";

        public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);
        }

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top