Actionscript 3 dispatch a custom Event
Don’t you hate it when you’re searching for something that should be really easy to find and implement though for the life of you you can’t find a good example?
I’ve just spent a fair amount of time trying to find a good example of dispatching a custom event in actionscript 3… mostly without any luck, at long last after a lot of searching I found a link in a comment on assertTrue which pointed me towards the Flex documentation… after which I was able to find something that helped.
So, to help those of you that might possibly be searching, and also no doubt to remind me when I next forget how to do it… below is how to dispatch a custom event class in Actionscript 3.
Firstly lets create the custom event class that will be dispatched, in my case what I wanted to do was to create a custom data loading status event. I probably could have instead used existing event classes to achieve the same thing, though it’s about time that I really nailed dispatching custom event classes.
package projectsomewhere.events
{
import flash.events.Event;
public class DataEvent extends Event{
public static var LOADED:String = "loaded";
public static var LOADING:String = "loading";
public function DataEvent(type:String):void{
super(type, true); // remove true if you don't want your event to bubble
}
override public function clone():Event {
return new DataEvent(type);
}
}
}
Now to actually dispatch this event… it’s rather simple!
When the program starts loading the data I run the following piece of code:
this.dispatchEvent(new DataEvent(DataEvent.LOADING));
When the data has loaded I execute this bit of code:
this.dispatchEvent(new DataEvent(DataEvent.LOADED));
Now to listen for these events is simple, just add along the lines of the following:
this.loaderobject.addEventListener(DataEvent.LOADING, itsLoading); this.loaderobject.addEventListener(DataEvent.LOADED, itsLoaded);
What you can do with your new custom event class is that you can then add custom properties and methods to it which are then available within your event handler. As an example, I could set a data property which I populate with the data prior to dispatching, then my event handlers can get easy access to the data that has been loaded.
When we are listening for and dispatching our events we could just as easily use “loading” instead of DataEvent.LOADING, this wouldn’t be a great idea though as the compiler will pick up on a typo for the latter… not for the former (bug heaven). As the docs say:
These constants provide an easy way to refer to specific event types. You should use these constants instead of the strings they represent. If you misspell a constant name in your code, the compiler will catch the mistake, but if you instead use strings, a typographical error may not manifest at compile time and could lead to unexpected behavior that could be difficult to debug. For example, when adding an event listener, use the following code:
myDisplayObject.addEventListener(MouseEvent.CLICK, clickHandler);
rather than:
myDisplayObject.addEventListener("click", clickHandler);
Filed by Popeye at January 3rd, 2008 under Flash, Flex, Actionscript 3