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:
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:
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:
next:
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.