You could set it up as a Singleton.
Basic structure:
public class ClassName {
private static var _instance:ClassName;
public function ClassName() {
// run normal constructor code here
}
public static function get instance():ClassName {
if ( !_instance ) {
_instance = new ClassName();
}
return _instance;
}
}
So instead of you ever calling new ClassName()
in your code, you just call ClassName.instance
to access the single instance of that class. That will return the same single instance every single time, and create it if it has not already been created. This will ensure there is never more than a single instance of the code at any given time (assuming you never call new ClassName()
of course)
Note that this is not a design pattern that should be used often, if at all. It goes against basic OOP principles and is a highly debated design pattern for that reason. I think this works in this case because you do not want more than one instance of your game running at any given time, but in most cases you should write your code to avoid this pattern.