Skip to content

Instantly share code, notes, and snippets.

@mimshwright
Created January 30, 2010 22:52
Show Gist options
  • Select an option

  • Save mimshwright/290770 to your computer and use it in GitHub Desktop.

Select an option

Save mimshwright/290770 to your computer and use it in GitHub Desktop.
using a whacky hack to create single instances of several classes at once.
/**
* creates a single instance of each state and stores them in a dictionary of states.
*/
// OH, HACKY! .... OR BRILLIANT?
protected function initializeStates():void {
allStates = new Dictionary( false );
//This vector contains several state classes i want to instantiate.
var stateClasses:Vector.<Class> = new Vector.<Class> ([MainMenuState, CreditsState, GameplayState]);
for each ( var stateClass:Class in stateClasses) {
allStates[stateClass] = new stateClass();
}
}
/**
* Gets the instance of the state based on the class.
*/
public function getState(stateClass:Class):IGameState {
return allStates[stateClass] as IGameState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment