Build 10 classic Flash games and learn game development along the way with this ultra-fast paced game development course.

If you love this blog, this is the book for you.

Buy the book

Get the source code of 12 commercial Flash games, which have been loaded more than 50 million times!

Learn from real world successful examples.

Get it now

Box2D for Flash Games teaches you how to make Flash physics games from scratch with the most advanced features.

Create the new Flash game smashing hit.

Buy the book

Understanding AS3 getDefinitionByName (for all eval maniacs)

Sometimes you may want to create a new class or DisplayObject only starting from its name. This is where something like an eval function would come into play, but we have to manage it in a different way with AS3.

Look at this movie:

Circles are four different objects called symbol1, symbol2, symbol3 and symbol4 and this is the script:

Obviously it’s a bit unoptimized, just think about if we had a thousand objects. That’s when getDefinitionByName comes into play. It returns a reference to the class object of the class specified by the name parameter.

This means the same script can be written this way:

DisplayObjects names are passed as strings in getDefinitionByName method and created as generic classes that can be used anywhere in the movie.

If you want to see a real world example, cracks textures in my upcoming game have been made this way:

Starting from a random number I create the texture according to such number and the material name.

Something to keep in mind if you can’t use anything similar to eval.

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (13 votes, average: 4.77 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 13 comments

  1. Pierre Chamberlain

    on March 31, 2011 at 5:42 pm

    Quote:
    “”

    May want to look into the D.eval API:
    http://www.riaone.com/products/deval/index.html

    It provides a SWC you can add to your project in order to simulate the same behavior as eval(…).

    Good tip on using getDefinitionByName though! It may be important to mark somewhere in your article how this can be used in Child SWFs for looking up any embedded classes in the Parent SWF, instead of compiling them in the each individual Child SWFs (which would increase overall project filesize).

    :)

  2. Pierre Chamberlain

    on March 31, 2011 at 5:47 pm

    Quote:
    “Something to keep in mind if you can’t use anything similar to eval.”

    May want to look into the D.eval API:
    http://www.riaone.com/products/deval/index.html

    It provides a SWC you can add to your project in order to simulate the same behavior as eval(…).

    Good tip on using getDefinitionByName though! It may be important to mark somewhere in your article how this can be used in Child SWFs for looking up any embedded classes in the Parent SWF, instead of compiling them in the each individual Child SWFs (which would increase overall project filesize).

    :)

    EDIT: Sorry about the double post, forgot to include what I was quoting on your article!

  3. Izik Bachar

    on March 31, 2011 at 8:55 pm

    It true when you are in the same package
    if the classes you want to “eval”
    are placesd in a diffrent package you need first or
    - do import for the package with “*” sign to import all the classes you want to eval
    -you need to use registerClassAlias method

  4. Niraj

    on April 1, 2011 at 3:46 am

    thanks for the tutorial
    if you want to do more than getDefinitionByName then consider using
    lang and reflect packages of http://www.as3commons.org/

  5. Jorge Dourado

    on April 1, 2011 at 12:02 pm

    Simple&clean explanation!
    Great tip ;)

  6. ViolentAJ

    on April 3, 2011 at 5:52 am

    The getDefinitionByName method is a very useful. When I first started AS3 a couple of years ago, I was disappointed because I didn’t know how to port my old spawning methods from AS2 (which used the attachMovie that allowed us to use the linkage name from the library unlike addChild).

    This is going to be helpful for a lot of developers moving from AS2 to AS3 game design.

  7. Philippe

    on April 4, 2011 at 9:03 am

    Might be worth explaining how to use that using Flex SDK and pure AS3 projects – beginners will be very confused that this doesn’t work because the classes won’t be compiled automatically (ie. no “Export in 1st frame” as in Flash Pro).

  8. Yarden Refaeli

    on April 12, 2011 at 8:07 pm

    Don’t forget that eval is a bad practice usually. Eval is evil. Well, at least in the HTML JS world. Why not to use arrays?

  9. Fred

    on August 29, 2011 at 1:56 pm

    Hi!

    thx for the code, but why wont work with other classes?

    package com
    {
    import flash.display.Sprite;
    import flash.utils.getDefinitionByName;

    public class DefinitionSample extends Sprite
    {
    public function DefinitionSample()
    {
    var ClassReference:Class = getDefinitionByName(“com.Template”) as Class;

    }
    }
    }

    // ReferenceError: Error #1065: Variable Template is not defined.

    thx!
    Fred

  10. Jorge

    on September 7, 2011 at 12:11 pm

    Yeap! I agree with Fred. There is a lack on this article… how to use it with custom classes ? (and not just the ones from as3)

    Is the solution the one that “Izik Bachar” stated ?


    Jorge

  11. Matz

    on January 3, 2013 at 8:34 am

    Am new to as3 if someone got the solution to why fred codes not working it would be of a great help to me

  12. jackie

    on April 13, 2013 at 11:53 am

    Hi
    i used this code,but it didn’t work and throw “Error #1065: Variable Head1 is not defined”
    Head1 is the class linkage name i defined in flash,and i publish it as swc.then link to my flash builder 4.6 .could you tell me whats going on?

  13. ThatCoderGuy

    on April 28, 2013 at 5:49 am

    For those having trouble with getting the example to work, and to answer Fred’s question:

    You are likely running into an issue where the class in question has no clue what you are talking about in your “getDefinitionByName” call. To use Fred’s provided code as an example:

    package com
    {
    import flash.display.Sprite;
    import flash.utils.getDefinitionByName;
    import com.Template;

    public class DefinitionSample extends Sprite
    {
    Template;

    public function DefinitionSample()
    {
    var ClassReference:Class = getDefinitionByName(“com.Template”) as Class;
    }
    }
    }

    This should now work. The reason for this is because at the time of trying to retrieve the definition, the application did not know what Template was. By invoking it at the beginning of the class (or anywhere before getDefinitionByName) we are essentially letting DefinitionSample know that Template actually exists.

    This issue arises because when ActionScript is compiled it automatically cleans up any references to classes that it thinks you don’t need, and since Fred wasn’t implicitly calling Template earlier, it was viewed as unneeded. This is a gotcha that has caught many off-guard including myself, and is generally regarded as a bug with AS3, though it does make a bit of sense.

    I hope you found this helpful and that the example I provided works. I didn’t actually test compiling it and I am running of memory a bit here.