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.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <pre>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.
  • E-Mail addresses are hidden with reCAPTCHA Mailhide.

More information about formatting options

Complete this form and then pat yourself on the back.