I was writing some control classes in AS 3 the other day and I realised I needed to create a custom event. I hadn’t done this in quite a while as my new duties don’t give me as much time to code as I’d like :( . I had forgoten just how different it is from AS2. So, for my own sake (as the old memory isn’t what it used to be) and for those of you who don’t already know, here’s how to create and dispatch a custom event in ActionScript 3.0.The dispatchEvent functionality that you may have used in AS 2.0, for bespoke event broadcasts and extended event object handling, has been changed quite a bit for AS 3.0. I used dispatchEvent quite a lot in my work before AS 3.0 came along. Frankly it’s been invaluable. Much of the time I want to dispatch events that are not of the predefined variety. Or I need to send off specific, bespoke data with the event when it’s fired. Usually it’s both of these requirements. Some of my colleagues even wrap predefined events inside EventDispatchers as a method of extending the event.

So before I get to how it’s done in AS 3.0, let’s recap with an over view of how it’s done in AS 2.0, so we have a comparison.

AS 2.0

Generally you would import the Delegate and EventDispatcher classes:

import mx.utils.Delegate;
import mx.events.EventDispatcher;


Then you would set up a generic event dispatcher associated object:

public var objBroadcaster:Object;

Next you would instantiate and initialize the event dispatcher associated object.

this.objBroadcaster = new Object();
EventDispatcher.initialize(this.objBroadcaster);

The next step was to add event listeners. This would set up the event dispatcher associated object to listen for any number of bespoke events. In the addEventListener declaration you would specify the event type to listen for, followed by the event handler function that would handle the broadcast event and it’s data object:

objBroadcaster.addEventListener(”eventType”, Delegate.create(this, this.eventHandler));


I personally, often use a generic method for dispatching events to the event listeners. See below. But the principal is exactly the same. The dispatchEvent method broadcasts an event type occourance and it’s associated event data object (in our example below this is a variable or object called param).

private function dispatchCaller(eventName:String, param:Object){
dispatchEvent({type:eventName, param:param});
}

Finally, the event handler function would be called, receiving the event data object (with however much or little data you had included).

All nice and bespoke, nice and flexible.

AS 3.0

AS 3.0 is a different beast. It is based heavily on the event model, so it is natural that the event should take a primary position in this.

First, we create our new bespoke event class by extending the Event class itself. I have an example below that shows us creating an event class called MediaControlEvent.

package {
import flash.events.Event;

public class MediaControlEvent extends flash.events.Event {
public static const CONTROL_TYPE:String = “headControl”;
private var command:String;

public function MediaControlEvent( command:String ) {
super( CONTROL_TYPE);
this.command = command;
}
}
}


You’ll see we extend this class from the Event class. We define a public static constant variable called CONTROL_TYPE. This stores the string representation of the event type (no prizes for those of you who guessed that). We then define the command variable. This is our bespoke data extension to the Event class, and in our example, this too handles a string. We could of course extend it as much as we like and call the variables what ever we want.

The constructor receives a string and passes that through to the local command variable. Also in the constructor, we call the Event class constructor (super), passing it the CONTROL_TYPE variable.

The class which dispatches this event will need to import the following classes:

import flash.events.EventDispatcher;
import flash.events.Event;
import MediaControlEvent;


In the same class, just as with the AS 2.0 example, we need to add an event listener for our new MediaControlEvent.CONTROL_TYPE event and assign an event handler to respond to our MediaControlEvent.CONTROL_TYPE event broadcasts.

addEventListener(MediaControlEvent.CONTROL_TYPE, eventHandler);


Then, at the appropriate time (button press for example), we dispatch our event and our associated data.

dispatchEvent(new MediaControlEvent(”RW”));

In the example above I’m simply dispatching a single string data with the event (as defined in our extended event class), but you could obviously dispatch any amount of data of any type, provided your event class was extended to handle them.

Our event listener will hear this, fire off the registered event handler and pass on the accompanying data, and the event handler will deal with the data as necessary. In the example below it will just trace out the string we passed to the event.

public function eventHandler(evt:MediaControlEvent):void{
trace(evt.command)
}

So, I hope this helps with creating and dispatching bespoke events in AS3. I know it took me a while at first to figure out how they were done, but when I ws finished I realise how little code there actually was involved (it’s mostly in the extended Event class).