I'm trying to use a .SWC library (exported from a .FLA document) to store graphical data for a game. In one of my classes I'm trying to attach an instance of the requested level's MovieClip, but I'm trying to use getDefinitionByName() so I can pull in the correct class based on the level number. I'm working in Flash Builder 4.7, and the SWC in question is pulled in as a build path library set to "Merged into code," in theory and up until now in practice making its classes accessible from anywhere.
However, getDefinitionByName() isn't working, even when I can confirm that the class it evaluates to exists and is freely accessible.
Below is kind of what I'm dealing with in my class constructor.
1:
public function MyClass() {
var lev:MovieClip = new Level1();
}
2:
public function MyClass(id:uint) { // For this example, id == 1
var lClass:Class = getDefinitionByName("Level"+id) as Class;
var lev:MovieClip = new lClass();
}
In theory, #1 and #2 should produce exactly the same result, namely, "lev" is a new instance of the Level1() class, right? But #1 works and #2 throws ReferenceError: Error #1065: Variable Level1 is not defined.
Even more inexplicably, I've also gotten almost exactly the same thing to work in a method of this very same class, the only difference being that said method calls a static method of a different class, which in turn calls getDefinitionByName(). Is the static method making the difference, and if so, why?
I've never used the getDefinitionByName(), but a quick look at the LiveDocs makes it look like you need to provide a full package path.
var lClass:Class = getDefinitionByName("Level"+id) as Class;
should be
var lClass:Class = getDefinitionByName("com.your.package.here.Level"+id) as Class;
See getDefinitionByName()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With