3

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?

Eric N
  • 218
  • 3
  • 13
  • Wait a minute, did you read my mind? I'm doing this same thing and have that same class name. – Cilan Aug 01 '14 at 19:45

1 Answers1

6

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

Josh
  • 8,079
  • 3
  • 24
  • 49
  • Turns out that was the problem. I had recently specified packages for my symbols, since Flash Pro doesn't bother with packages if you simply export a symbol for ActionScript. I tried simply importing those packages (i.e. import com.my.package.levels.*;), but it turns out that wasn't enough; the package path does need to be built into the getDefinitionByName() argument. – Eric N Jan 30 '13 at 18:13
  • This is the case for almost all instances where you need to provide a class name in the form of a string, especially on the Flex side of things. Unless you are passing the class itself, just pass the entire path. Flash gets confused if you don't because there can be multiple classes with the same name in different packages. – Josh Jan 30 '13 at 18:15
  • What if the package name is 'default package'? – ness-EE Mar 25 '14 at 16:01
  • @FusePumpDev You should *never* have classes in the default package. The only class that, at least in my eyes, is okay to be in that package is the application class, which itself should never be more than a launching ground. It sounds like you need to reorganize your project to me. – Josh Mar 25 '14 at 16:03
  • @JoshJanusch - importing an SWC with assets in from Flash Pro has them in default package. I can't see how to change it. – ness-EE Mar 25 '14 at 16:24
  • ... not the project's default package; in the referenced libraries directory, like Referenced Libraries/assets.swc/default package/Button.abc – ness-EE Mar 25 '14 at 16:31
  • Please see http://stackoverflow.com/questions/22647257/movieclip-assets-swc-from-flash-inaccessible-in-flash-builder-using-getdefinitio – ness-EE Mar 25 '14 at 22:32