How to Send Custom Events

admin  

You can dispatch an event from any class that extends EventDispatcher. That includes Sprite and Movieclip classes.

First of all create a custom event class:

package custom.events
{
    import flash.events.Event;

    public class CustomEvent extends Event
    {
        public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);
        }
        
        public static const OK:String = "ok_result";
    }
}

In your class add the code to dispatch the new event:

public class CustomSprite extends Sprite
    {
        public function doSomething():void
        {
            this.dispatchEvent(new CustomEvent(CustomEvent.OK));
        }
    }

Now all you have to do is to add a listener for your custom event:

public class MainSprite extends Sprite
    {
        public function MainSprite()
        {
            var s:CustomSprite = new CustomSprite();

            s.addEventListener(CustomEvent.OK, onOk);
        }

        public function onOk(event:CustomEvent):void
        {
            trace("custom event occured.");
        }

    }

If you have to send additional data when the event occurs you can add new member variables to the event.