Created
September 24, 2011 21:06
-
-
Save jacksonkr/1239861 to your computer and use it in GitHub Desktop.
Static Listeners in Action Script 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* MyPlayer class | |
*/ | |
package { | |
public class MyPlayer extends MovieClip { | |
/** | |
* This static variable will hold our pending listeners for the class. | |
* They will be instantiated once the constructor runs. | |
*/ | |
private static var _pending_listeners:Array = []; | |
/** | |
* This is the static reference for our singleton. | |
*/ | |
public static var instance:MyPlayer; | |
public function MyPlayer() { | |
MyPlayer.instance = this; | |
for each(var listener:Object in MyPlayer._pending_listeners) { | |
this.addEventListener(listener.type, listener.func); | |
} | |
} | |
public static function addEventListener(type:String, func:Function):void { | |
if(MyPlayer.instance) MyPlayer.instance.addEventListener(type, func); | |
else MyPlayer._pending_listeners.push({type:type, func:func}); | |
} | |
} | |
} | |
// From your play button you would add a listener to the non-instantiated class by doing the following: | |
MyPlayer.addEventListener(SomeEvent.PLAYING, playerPlayingHandler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment