FlashDevelop – Fixing getDefinitionByName ReferenceError: Error #1065

This is a pretty common error to come across when you first start using FD but I thought it might help someone. Basically when you try to use a class that you have included in your SWC (such as a Bitmap or Sound with a linkage tag) using getDefinitionByName(“MyClassName”) as Class, the compiler throws an error message saying it can’t find them – variable not defined etc. Even though you think that those classes are in your SWC and therefore in your project, the compiler still doesn’t know about them so you need to tell it to include them when compiling.

There are several ways to achieve this. The most basic way is just to reference them in your code. Even a trace(MyClass) message will work or defining a dummy variable like var foo:MyClass; . The problem with this method is that you have to write a lot of dummy code to include a bunch of classes.

The correct way to do it is to actually add the SWC that the class is in to the project via Project Properties > Compiler Options > SWC Include Libraries list. This way all classes in the swc you specify are compiled. However there problem with this method is that this it includes ALL the classes in that SWC you define.

If like me you are using different classes as you develop and you don’t want to include a whole bunch of massive images the best way I have found is to include a static array with a list of the classes you want to include. So for example if I want to include a bunch of images in my SWC and call them using a string via getDefinitionByName(“MyClassName”) as Class I can force them to be included using:

private const includeClasses:Array = [MoonBMP, LavaBMP, CracksBMP];

and then later I can grab them using a string:

var ImageClass:Class = getDefinitionByName("MoonBMP") as Class;
var img:Bitmap = new ImageClass() as Bitmap;

Sorted!