Skip to content

Instantly share code, notes, and snippets.

@mhulse
Last active November 9, 2015 18:10
Show Gist options
  • Save mhulse/dbc517203ceddda2f675 to your computer and use it in GitHub Desktop.
Save mhulse/dbc517203ceddda2f675 to your computer and use it in GitHub Desktop.
Singleton class I use for AS3 projects.
// https://gist.github.com/mhulse/dbc517203ceddda2f675
package com.ieqtech {
import flash.display.*;
import flash.events.*;
// http://stackoverflow.com/a/13318889/922323
public final class Singleton extends MovieClip {
private static var _instance:Singleton;
public function Singleton() {
if (_instance) {
throw new Error('`Singleton()` already instantiated! Use `Singleton.getInstance()` instead.');
}
_instance = this;
this.init();
}
public static function getInstance():Singleton {
if ( ! _instance) {
new Singleton();
}
return _instance;
}
private function init():void {
(this.stage) ? main() : this.addEventListener(Event.ADDED_TO_STAGE, main, false, 0, true);
}
private function main($event:Event = null):void {
if ($event !== null) {
this.removeEventListener(Event.ADDED_TO_STAGE, init);
}
trace('Hello world!');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment