How to dispatch a custom event in Actionscript 3

This is going to be a very simple example of how to dispatch an event. In actionscript 3, you dispatch an event by extending the EventDispatcher class. I’m not going to get into a big complicated explanation right now, but I will show you a couple of simple examples.

One good thing about the Sprite class is that it already extends the EventDispatcher class, so if you are working from your document class, or your class extends Sprite/Movieclip, you can use this anywhere in your class:

1
2
3
4
// Declare at the top...
public static const CUSTOM_EVENT:String = "customEvent";
// ... Somewhere in your class...
dispatchEvent(new Event(CUSTOM_EVENT));

Let’s say for example you have the following class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package{
   
    import flash.display.Sprite;
    import flash.events.EventDispatcher;
   
    public class Cat extends Sprite{
       
        public static const MEOW:String = "meow";
       
        public function Cat(){         
        }
       
        public function meow(){
           
            dispatchEvent(new Event(MEOW));
        }
    }
}

Now create an instance from your timeline:

1
var cat:Cat = new Cat

next:

1
2
3
4
5
6
cat.addEventListener(Cat.MEOW, doSomething);
cat.meow();

function doSomething(event:Event){
    trace("The cat is hungry!");
}

Ok, so this really doesn’t do anything useful, but you can see how you could use this idea to broadcast events from your objects. This is by no means comprehensive, but I have noticed that this simple concept has eluded a lot of people. Later I will write a more robust tutorial showing how to extend the EventDispatcher class and do some cool stuff.

Share on FacebookShare on Google+Share on StumbleUponTweet about this on TwitterEmail this to someone

Facebook comments:

9 thoughts on “How to dispatch a custom event in Actionscript 3”

  1. Nice, I didn’t know of this. It would be useful for event listeners and all.
    I see a wide variety of useful scripts someone could make with this.

  2. You can have your class broadcast all kinds of events for a variety of different situations and listen from other classes. It is especially useful when you need to wait for something to finish before starting something else. (such as my flashPHP class). It broadcasts a “ready” event after it receives the data from php.

  3. Using a simple String like this is bad practice.

    It leaves the dev wide open for typo’s, guessing what they need to listen to (you should add metadata to describe what events will be dispatched) and skips all type checking on the event

  4. Tink, you make a couple of great points and you are absolutely correct.

    Technically I probably should have created a new custom event class, such as the one in your example (which is very good, everyone check it out here:
    http://www.tink.ws/blog/custom-events-in-as-30-dont-forget-to-override-the-clone-method

    For my blog I try to create the most simple of examples so that people can get a grasp of concepts that they haven’t been able to get their heads around. Unfortunately sometimes it is at the cost of best practice. Thanks for your comment!

  5. Tink, the author’s right: shut up and cram your anal retentiveness. This simplified example is what most of us need to get going. In fact it’s all most beginners will ever need. When we’re ready to do it “right” we’ll check your blog for your version, but until then spare us your pedantic comments.

  6. very nice, the most simple example to understand custom events, But what I think is you should also write the actual usage.
    as a learner I would question you why did you add

    1
     cat.addEventListener(Cat.MEOW, doSomething);

    instead I can trace

    1
     trace("The cat is hungry!");

    in cat.meow()Method/Function.

    with these kind of examples we can understand the usage but we ignore CUSTOM EVENTS, as its real importance is not being explained.

    anyways good example for beginners..

    Thanks,
    RUDRA PRADEEPTA K.
    FLASH DEVELOPER.

  7. Rudra:

    Thanks for your comment!

    I realize that you could simply trace(“The cat is hungry!”); but that would miss the point of how you can point to a function after dispatching a custom event.

    Maybe I’ll make another article with a more significant result from dispatching an event.

    Thanks!

Leave a Reply