# \[AS3\] Custom Event problems

**URL:** <https://forum.kirupa.com/t/as3-custom-event-problems/223412>\
**Category:** flash\
**Created:** [April 22, 2007, 1:16pm UTC](https://forum.kirupa.com/t/as3-custom-event-problems/223412 "2007-04-22T13:16:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![prisma](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@prisma](https://forum.kirupa.com/u/prisma)\
**Post date:** [April 22, 2007, 1:16pm UTC](https://forum.kirupa.com/t/as3-custom-event-problems/223412/1 "2007-04-22T13:16:20Z")

</div>

Hi!

I’m trying to make a custom event that bubbles up from the Items object in this code to the Main object, and start  
the changeInfoHandler there.

For some reason, it works when I put the dispatchEvent in the Items object inside a handler for a mouse click,  
but when I try to bypass that and call the function directly, it seems to only work in the scope of the Items object.

Main.as:

```auto

package
{
    import flash.display.*;
    import flash.events.*;
    import ChangeEvent;
    
    [SWF(backgroundColor="0xefefef", frameRate='60', width='640', height='480')]

    /**
    * @mxmlc -use-network=false -library-path+=./ -source-path+=./ -locale=en_US
    */

    public class Main extends Sprite
    {                    
        public function Main()
        {
            addEventListener(ChangeEvent.INFO, changeInfoHandler);

            // this is for senocular's Output.as trace Class
            stage.addChild(new Output(stage));
            initItems();
        }

        private function initItems():void
        {
            var items: Items = new Items();
            addChild(items);
        }
        
        public function changeInfoHandler(evt: ChangeEvent):void
        {
            Output.trace("we're in the changeInfoHandler of " + this + "(" + evt.target + evt.currentTarget + evt + ")");
        }
    }
}

```

Here’s the ChangeEvent.as

```auto

package
{
    import flash.events.*;
    
    public class ChangeEvent extends Event
    {
        public static const INFO:String = "info";
        
        public function ChangeEvent(type: String, bubbles: Boolean, cancelable: Boolean)
        {
            Output.trace("we're in the ChangeEvent.");
            super(type, bubbles, cancelable);
        }
        public override function clone():Event {
            return new ChangeEvent(type, true, false);
        }
    } 
}

```

The follow Items.as works:

```auto

package
{
    import flash.display.*;
    import flash.events.*;
    import ChangeEvent;
    import flash.text.TextField;
    
    public class Items extends Sprite
    {
        public function Items()
        {
            addEventListener(ChangeEvent.INFO, changeInfoHandler);
            addEventListener(MouseEvent.CLICK, mediate);
            
            makeItems();
        }
        private function makeItems():void
        {
            var dummyField: TextField = new TextField();
            dummyField.text = "here's something to click";
            addChild(dummyField);
            // mediate();
        }
        
        // private function mediate():void
        private function mediate(evt: MouseEvent):void
        {
            var changeEvent:ChangeEvent = new ChangeEvent(ChangeEvent.INFO, true, false);
            dispatchEvent(changeEvent);
        }
        
        public function changeInfoHandler(evt: ChangeEvent):void
        {
            Output.trace("we're in the changeInfoHandler of " + this + "(" + evt.target + evt.currentTarget + evt + ")");
        }
    }
}

```

traces:

> 

we’re in the ChangeEvent.  
we’re in the changeInfoHandler of [object Items]([object Items][object Items][Event type=“info” bubbles=true cancelable=false eventPhase=2])  
we’re in the changeInfoHandler of [object Main]([object Items][object Main][Event type=“info” bubbles=true cancelable=false eventPhase=3])

The following Items.as doesn’t work:

```auto

package
{
    import flash.display.*;
    import flash.events.*;
    import ChangeEvent;
    import flash.text.TextField;
    
    public class Items extends Sprite
    {
        public function Items()
        {
            addEventListener(ChangeEvent.INFO, changeInfoHandler);
            
            makeItems();
        }
        private function makeItems():void
        {
            var dummyField: TextField = new TextField();
            dummyField.text = "here's something to click";
            addChild(dummyField);
            mediate();
        }
        
        private function mediate():void
        {
            var changeEvent:ChangeEvent = new ChangeEvent(ChangeEvent.INFO, true, false);
            dispatchEvent(changeEvent);
        }
        
        public function changeInfoHandler(evt: ChangeEvent):void
        {
            Output.trace("we're in the changeInfoHandler of " + this + "(" + evt.target + evt.currentTarget + evt + ")");
        }
    }
}

```

traces:

> 

we’re in the ChangeEvent.  
we’re in the changeInfoHandler of [object Items]([object Items][object Items][Event type=“info” bubbles=true cancelable=false eventPhase=2])

Any ideas why?

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [July 8, 2008, 2:26pm UTC](https://forum.kirupa.com/t/as3-custom-event-problems/223412/2 "2008-07-08T14:26:22Z")

</div>

looks to me like you’re trying to dispatch the event before your object is added to the display list. Without being on the display list, the event can’t bubble
