ActionScript Custom Event Tutorial Part II
This post refers to AS3 Custom Event Tutorial
K, so you’re now extending the event object, having all sorts of fun, bouncing events all up and down your object tree like the destructive monkey that you are. But the question remains: How are you going to capture the events that you are dispatching? The simple answer: the event listener…which may be the most common construct in AS3. The event lister take the following form, where the eventHandler is the function we want to handle the event:
someObject.addEventListener(eventType,eventHandler);
private function eventHandler(event:eventClass):void{
//do something fun here;
}
If you go back to our last post in this series, you will see that we have extended the basic flash event class into an event type that can carry some data in the form of an array. In this case, we are using it for a hypothetical user login i.e. if the user is authenticated, then our extended event, MyRemoteEvent is dispatched like so:
dispatchEvent(new MyRemoteEvent(MyRemoteEvent.LOGIN_SUCCESS,true,false,infoArray));
To sum up the above we are dispatching an event that is of type LOGIN_SUCCESS, does bubble and is not cancelable. the infoArray argument is any login information that we might be getting from the server. Likewise, if the user login attempt results in a login failure, we dispatch the custom event as so:
dispatchEvent(new MyRemoteEvent(MyRemoteEvent.LOGIN_FAIL,true,false,null));
The important thing to remember here is that the event types you are specifying when you dispatch these events are defined as constants in your event class. You can specify a discrete handler for each possible type of event as in the following:
someObject.addEventListener(MyRemoteEvent.LOGIN_SUCCESS,loginEventHandler);
someObject.addEventListener(MyRemoteEvent.LOGIN_FAIL,failEventHandler);
or you can group the handler functions in sensible groups, and inspect the type property of the event as it comes in as so:
someObject.addEventListener(MyRemoteEvent.LOGIN_SUCCESS,genericEventHandler);
someObject.addEventListener(MyRemoteEvent.LOGIN_FAIL,genericEventHandler);
private function genericEventHandler(event:MyRemoteEvent):void{
if(event.type==’LOGIN_SUCCESS’){
//do something
}else{
//do something else
}
}
As you can see in the above, you could add a switch or another if statement to handle various cases that your application might be asked to handle in future. Just be careful not to make a huge mess in there with nested if statements or 50,000 conditions. And please, please comment your code.
Thats about it for this little tutorial, Happy Coding!
4 comments so far
Leave a reply
I’ve found it easier to store the event type string as a static property of the class that dispatches the event, rather than the custom event class itself. That way I don’t need to keep adding new event type properties to my generic custom event class each time I want to set up a new event I hadn’t previously thought of.
Alex, can you show us an example of this?
my custom event class (which I’ve called ArgEvent) is basically the same as yours, with an array used to store a parameter of whatever type. I’ve given it a default type parameter:
public static const GENERIC:String = “onGenericEvt”;
then any specific event type strings such as onScroll, onClick, onSelect, etc I will store as static properties of not the custom event class, but of the class that is dispatching the event.
for example, my vertical scrollbox class dispatches a scroll event, so it has the static property:
public static const SCROLLING:String = “onScroll”;
and it dispatches the event as follows:
dispatchEvent(new ArgEvent(SCROLLING,[_nScroll]));
if the type string was to be a property of the custom event class, I’d have had to add the line;
public static const SCROLLING:String = “onScroll”;
to the event class itself, and then dispatched it like this:
dispatchEvent(new ArgEvent(ArgEvent.SCROLLING,[_nScroll]));
It’s a really trivial difference really, but I just found that I kept coming up with new event types and having to add them to the event class each time was becoming a pain in the @rse! I also wanted to keep my custom event class tidy and as generic as possible.
I only mention it here because I haven’t had a chance to do much AS 3.0 stuff yet, so I thought you’d probably tell me if I’m doing something dumb!
cheers,
Alex
please give us in video tutorial.though it was good.