getDefinitionByName error: “Variable … is not defined”
I wrote this post mainly as a reminder for myself, but it might be useful for anybody dealing with the same issue. In ActionScript3 you can get a class by its name like this:
import flash.utils.getDefinitionByName; ... var myClass = getDefinitionByName("MyClass") as Class;
When you only use the name of the class, you will get an error: “Variable … is not defined”, because the Flash Player can’t find it at run time. There are several ways to make it work.
1. You can import the class and create a dummy variable. The dummy is needed, otherwise the compiler won’t add the class.
import domain.project.MyClass; ... private var _dummyClass:MyClass;
2. Instead of a variable you can also use the following notation.
import domain.project.MyClass; ... MyClass;
3. You can also use the Frame metadata tag:
import domain.project.MyClass; ... [Frame(extraClass="MyClass")]
All these solutions work, but you need to specify the classes you want to use up front.
4. Far more easy, elegant and flexible is to include the path of the class when calling getDefinitionByName. You won’t need to import anything and you don’t have to create dummy vars or use extra metadata tags.
var myClass = getDefinitionByName("domain.project.MyClass") as Class;






March 14th, 2010 at 5:32 pm
nice note, thx!, btw it’s “domain.project.MyClassMyClass” or “domain.project.MyClass” ?
March 14th, 2010 at 5:46 pm
Ah yes, you are right! It should be “domain.project.MyClass”. I updated it right away. Thanks!
March 16th, 2010 at 8:06 am
[...] getDefinitionByName error: “Variable … is not defined” [...]
March 29th, 2010 at 8:02 pm
var myClass = getDefinitionByName(“domain.project.MyClass”) as Class;
What do domain & project stand for? Example?
Thank you!
April 7th, 2010 at 9:49 pm
For example: flash.display.Sprite
April 12th, 2010 at 1:05 am
effing awesome. i wish i had known this three years ago. yay fully-qualified class names!
April 12th, 2010 at 1:06 am
ps. curtisch — those are packages. so in this example, MyClass is in package domain.project.MyClass:
package domain.project.MyClass {
public class MyClass () {
// …
}
}
April 12th, 2010 at 1:15 am
oi, sorry for the numerous comments. but this doesn’t seem to work unless you have a reference to the class created via getDefinitionByName somewhere in the class that creates it. you don’t need an import statement, just a reference — this worked on my first try, because i had a cast to the getDefinitionByName-created class later in my code. commenting out that cast (and therefore the reference to the class) broke the getDefinitionByName functionality.
any thoughts?
April 28th, 2010 at 8:39 pm
Hi ericsoco, sometime it doesn’t work out for me too and I can’t quite figure out why and how.
First I compiled it with a reference in the code. Then I remove it and I was amazed to find out that everything still seemed to work. But I’m having trouble to use it in other classes… Is there such a thing as a compiler history?