TimL
TimL

Reputation: 3

AS3 getDefinitionByName - Error #1065: Variable {class name} is not defined

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.

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.

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.

1.) Object(myInstance).constructor as Class

2.) getDefinitionByName(getQualifiedClassName(myInstance)) as Class

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.

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.

Does anyone have any suggestions?

Thanks!

Upvotes: 0

Views: 5845

Answers (1)

weltraumpirat
weltraumpirat

Reputation: 22604

  1. Make sure your Avatar.swf is loaded into the same application domain.
  2. Make sure you have used each of the avatar classes at least once (e.g. in the document class of Avatar.swf), or if they are Flash library items, export them into frame 1 of Avatar.swf.

Upvotes: 1

Related Questions