29 Nov 2010

FlashDevelop – Fixing getDefinitionByName ReferenceError: Error #1065

FlashDevelop 3 Comments

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;


3 Responses to “FlashDevelop – Fixing getDefinitionByName ReferenceError: Error #1065”

  1. Christen says:

    I don’t have a SWC but a folder full of files (that changes compile time) that I need to getDefinitionByName from, is it possible to add those classes to the compiler options. I have looked it over and I cannot find of the option that include a folder of classes.

  2. Patrick says:

    This solution works.

    In my approach, I consolidates all asset classes to be loaded from SWC in a helper class.


    package word.util{
    import background.*;

    import button.*;

    import character.male.*;

    import clothes.male.*;

    import component.*;

    import flash.display.MovieClip;
    import flash.utils.getDefinitionByName;

    import loading.*;

    import text.*;

    import tile.*;

    public class AssetHelper{
    private static const __photo:Array=[Head01, Head02,Head03, Head04];
    private static const __back:Array=[Grassland];

    private static const __body:Array=[character.male.Boy];


    And in other part of the project, I’ll just make use of this class to instantiate objects

  3. Kicker says:

    Thank you. 🙂

Leave a Reply