Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3: "Variable [x] is not defined" error when using getDefinitionByName()

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?

like image 480
Eric N Avatar asked Nov 30 '25 04:11

Eric N


1 Answers

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()

like image 74
Josh Avatar answered Dec 02 '25 18:12

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!