/ Published in: ActionScript 3
There are a variety of ways to create a Singleton in AS3 but because Adobe wants to be ECMA compliant, there's no way to do it (like in Java) using a private constructor. Writing a singleton this way will give you a runtime warning if something goes wrong.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package { public class Singleton { private static var _instance:Singleton; private static var _okToCreate:Boolean = false; public function Singleton() { if( !_okToCreate ){ throw new Error("Error: " + this + " is not a singleton and must be " + "accessed with the getInstance() method"); } } public static function getInstance():Singleton { if( !Singleton._instance ){ _okToCreate = true; _instance = new Singleton(); _okToCreate = false; } return _instance; } } }