AS3: "Error variable [x] is not defined" when using getDefinitionByName ()

I am trying to use a .SWC library (exported from a .FLA document) to store graphics data for a game. In one of my classes, I am trying to attach an instance of the requested MovieClip level, but I am trying to use getDefinitionByName () so that I can infer the correct class based on the level number. I'm working in Flash Builder 4.7 and the SWC in question is pulled as a build path library set to "Merged in Code", theoretically and still in practice makes its classes available from anywhere.

However, getDefinitionByName () doesn't work even when I can confirm that the class it is evaluating exists and is freely available.

Below is a description of what I have 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 give exactly the same result, namely: "lev" is a new instance of the Level1 () class, right? But # 1 works and # 2 throwsReferenceError: Error #1065: Variable Level1 is not defined.

Even more inexplicable, I got almost the same thing as in a method of this very class, the only difference is that the specified method calls a static method of another class, which in turn calls getDefinitionByName (). The static method makes the difference, and if so, why?

+3


source to share


1 answer


I've never used it getDefinitionByName()

, but a quick look at the LiveDocs made it look like you need to provide the full package path.

var lClass:Class = getDefinitionByName("Level"+id) as Class;

      

it should be



var lClass:Class = getDefinitionByName("com.your.package.here.Level"+id) as Class;

      

Cm. getDefinitionByName()

+6


source







All Articles