AS3 Singleton Woes

Howdy guys,

  I'm working on a game using a singleton implementation for my document class using an example I found a while back on the internet. Relevant Code posted below:



var instance:ReddEngine = null;
var allowCreation:Boolean;

public static var waypointlinks:Array;

public function ReddEngine()
        {                         
            if (!allowCreation)
                throw new Error( "Invalid Singleton access. Please use     ReddEngine.getInstance() instead." );
            else
            {
                instance = this;
                trace("Creating new ReddEngine");    
                waypointlinks = new Array();

        //the rest of the constructor
            }
}


public static function getInstance():ReddEngine {            
            if (instance == null)
            {                
                allowCreation = true;            
                instance = new ReddEngine();
                allowCreation = false;
            }
            
            return instance;                         
        }


The array waypointlinks is meant to contain the collection of Linkers for my scripted actors, though that’s not entirely important to the problem I’m having (at least I don’t think it is). The class, Linker, on construction creates a reference to the stage calling


var stageRef:Stage;
stageRef = ReddEngine.getInstance().stage;

to add itself properly later into the arrays created in the engine. The Linkers are added into the game by dragging and dropping MovieClips exported for actionscript in Flash from the library.

But when it gets to that part in the code, the console throws an error saying that my .swf has invalid data.

My guess is that I have a race condition of some kind relating to the instancing of the ReddEngine, multiple things trying to create a ReddEngine all at once, or something, but I can’t for the life of me figure out how to fix this.

Any thoughts/suggestions? I can post more details if needed.

   Losing hair,
        - Z