title: AS3 getDefinitionByName - Error #1065: Variable {class name} is not defined
body: <p>My issue is slightly more complicated than the title suggests. I work for a company that has in the region of 20 flash games. Our players also have avatars which they can customise and these avatars are used inside our games.</p>
  
  <p>We have a file, Avatar.swf which is loaded by our preloader and then passed into GameName.swf. I want to take this Avatar movieclip and create a factory class so we can have many instances of it inside our games.</p>
  
  <p>I can get an instance of the Avatar movieclip from the loader without a problem. There are two way I know of to get a constructor from an instance in AS3.</p>
  
  <p>1.) Object(myInstance).constructor as Class</p>
  
  <p>2.) getDefinitionByName(getQualifiedClassName(myInstance)) as Class</p>
  
  <p>1 works intermittently but about 1 time in 3 I get a null reference error thrown by the resultant instance. I have no idea why but it is not a consistent problem. It's almost as if the scripts are there but the assets library isn't. It happens both in debug mode and release mode. It also happens both when compiled using FlashDevelop and CS5.5.</p>
  
  <p>2 throws Error #1065: Variable Avatar is not defined. I understand this is because I have not imported the Avatar package and declared a dummy instance of Avatar but this is the whole point. If we change the avatar by adding a new item of clothing for example, we definitely do not want to have to go through our ever expanding list of games and recompile them all. In other words, I do not want the Avatar code in every game, it would negate the point of loading it in our preloader.</p>
  
  <p>Does anyone have any suggestions?</p>
  
  <p>Thanks!</p>