Static Listeners in Action Script 3

Hypothetical sitatuion: Let’s say you have a Flash/Flex AS3 video player and a play button which are nested inside separate parents. Your play button listens to when your player is PLAYING so that it can update it’s display accordingly (shows a play icon when the player is paused and shows a pause icon when the player is playing).

Let’s assume that the play button is getting constructed before the player. When you try to add a listener to your player singleton, you get an error because the player singleton hasn’t been constructed yet. How do you combat this in a systematic, yet elegant matter? I’m sure there are several ways, but here’s how I do it using static functions.

This strategy is straight forward and simple to understand. You simply store the listener (or event) on the class where it waits until the your singleton is constructed before introducing it’s self. I’ve given an example below of what this code looks like:

Viola. You can use this strategy in numerous ways. I recently used it to dispatch ready listeners on a singleton that didn’t exist yet.

You might ask, “Why wouldn’t I just call a function to the other object or something simple like that?” Well, I can reply nothing more than my strategy is for those, who like myself, have a twisted love for the function name ‘addEventListener’. The End.

Leave a comment

Your email address will not be published. Required fields are marked *