Home > ActionScript, Flash, WorkFlow > Code Tip: Event vs StatusEvent

Code Tip: Event vs StatusEvent

There will be times in ActionScript development that you will want to listen to some events fired from SomeChild class to listen into SomeParent class where you would think that extending Event class is not an option because you just want to listen to events and don’t want to receive any data along with it. (Yup, when you want to receive some data along with the event itself, that is when you need to extend the Event class. Otherwise, no need).

But since, listening to events is costly in terms of lines of code (because you have to assign a listener function for every single event even if you wanted to just call another function from within it), there is another way of avoiding those lines of code, which is to use StatusEvent rather than Event class when dispatching.

StatusEvent has a string property called code which can be used within a switch statement in one function and can save and modularise the whole event flow.

Consider this example:

this.addEventListener ( SomeChild.SOME_INIT_EVENT, onInit );
this.addEventListener ( SomeChild.SOME_COMPLETE_EVENT, onComplete );
private function onInit ( eventObject:Event ):void
// do something on Init
private function onComplete ( eventObject:Event ):void
// do something on Complete

The above is what you do normally when dispatching using Event class and thus listening becomes a hassle since you have to assign to each event type and write listener functions for them as well.

But if you used StatusEvent when dispatching, listening would be fairly easy.

this.addEventListener ( SomeChild.SOME_STATUS_EVENT, onStatus );

private function onStatus ( eventObject:StatusEvent ):void
switch ( eventObject.code )
case SomeChild.SOME_INIT_EVENT:

// do something on Init


// do something on Complete

I hope this example provides a simpler and easy way of listening events when you only need to dispatch events without any custom data object.

Btw, here is how you would dispatch these events from within class:

this.dispatchEvent ( new StatusEvent ( SomeChild.SOME_STATUS_EVENT, false, false, SomeChild.SOME_INIT_EVENT ));
this.dispatchEvent ( new StatusEvent ( SomeChild.SOME_STATUS_EVENT, false, false, SomeChild.SOME_COMPLETE_EVENT ));

Thanks to Phillip Bulley for this idea.

About these ads
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: