FlashDevelop Singleton Template


Posted Nov. 8, 2009 in AS3, FlashDevelop at 1 p.m.

Putting the discussion of whether Singletons should even be used aside, this is just one of those things that no one wants to type out each time they need a Singleton. If you use FlashDevelop (and you should!) you might find this useful.

There are a number of Singleton templates out there, but I like mine. (Surprise surprise.) Even if you don't, it might be useful to pick apart and create your own.

A couple things to keep in mind:

1: $(CSLB) (”Coding Style Line Break”) makes it so that your braces will be cuddled or uncuddled based on your preferences. No need to uncuddle them yourself -- you'll get a double return.

2: $(EntryPoint) is, as far as I can tell, what causes FlashDevelop to automatically open the file when you create it. Without this you have to open it yourself, and that's annoying.

To add this to your template list, just go to the FlashDevelop program files folder (which can be accessed from inside FD by going to Tools --> Application Files... and then create a new folder inside the Templates\ProjectFiles\AS3Project directory. That's about all there is to it. Call it WhateverYouWant.as. (Singleton might be a good place to start.)

package $(Package) $(CSLB) {
	public class $(FileName) $(CSLB){
		//----------------------------------
		//  SINGLETON INSTANCE
		//----------------------------------
		private static var _instance:$(FileName);
		$(EntryPoint)
		//----------------------------------
		//  MEMBER VARIABLES
		//----------------------------------
		
		//----------------------------------
		//  PROTECTED CONSTRUCTOR
		//----------------------------------
		public function $(FileName)(enforcer:SingletonEnforcer) $(CSLB){
			if (enforcer == null)
			{
				throw new Error("Singleton: Do not instantiate this class with the constructor.");
			}
		}
		
		//----------------------------------
		//  GET INSTANCE
		//----------------------------------
		public static function getInstance():$(FileName) $(CSLB){
			if(!_instance)
			{
				_instance = new $(FileName)(new SingletonEnforcer());
			}
			
			return _instance;
		}
		
		//----------------------------------
		//  MEMBER PUBLIC METHODS
		//----------------------------------
		
		//----------------------------------
		//  MEMBER PRIVATE METHODS
		//----------------------------------
	}

}

class SingletonEnforcer { }


0 Comments



(Markdown enabled.)

Related Posts


Profiling live Flash applications

How to use the Flex/Flash Profiler on live Flash, even if it's totally a non-Flex project. (Like FlashDevelop projects!)


AS3 Flash sounds cutting off

A ridiculous fix for fixing issues with Flash MP3 sounds cutting when they're playing


Firefox Flash lock during load...

I'm experiencing a second or two where Firefox completely freezes during Flash load, any ideas?


Global sound volume in Flash AS3

I manage to forget immediately forget this after I do it once on every project involving sound. NO LONGER! (I hope.)


Greensock Tweening Platform v11

Jack just released the latest version of his Tweening Platform -- be sure to update!


Flash on the iPhone - the real story.

Some real facts and details regarding Flash apps running natively on the iPhone.




I'm Ryan Ragona, an Interactive Developer based in Seattle, and I write about AS3, Django, and more.

Hey game industry people, anyone have a line on SC2 beta keys? I'm dying here. :P posted 1 week ago from Tweetie
You should follow
me on Twitter!