GetDefinitionByName does not find a variable that exists

I am using flash.utils.getDefinitionByName in an attempt to capture an art resource. I use this function quite a bit and still have not had problems. Check this:

assetName = Assets.MegaBerry; // works
assetName = getDefinitionByName("Assets.MegaBerry") as Class; // doesn't work

What the hell? The error response for the second line: "Variable not found."

If it matters: Assets is the file in the root source directory (it does not have the package; Assets is the full name), and I tried to put:

import Assets;

upstairs with no luck.

For reference, in Assets.as I have:

    [Embed(source = "../art/Inventory/MegaBerry.png")]
    public static var MegaBerry:Class;
+3
source share
5 answers

, Assets Class, , - , Assets.MegaBerry: (!) .

MegaBerry . - - registerClassAlias - :

registerClassAlias("Assets.MegaBerry", Assets.MegaBerry);

, getDefinitionByName.

** EDIT **

, - ... , , , , {className}_{variableName} , . , :

getDefinitionByName("Assets_MegaBerry") as Class;

.

registerClassAlias , getClassByAlias getDefinitionByName. .

** END EDIT **

Embed, , getDefinitionByName :

package assets {

    [Embed(source="../art/Inventory/MegaBerry.png"]
    public class MegaBerry extends BitmapData {
    }
}
+3

assetName = getDefinitionByName("Assets.MegaBerry") as Class;

:

assetName = Assets["MegaBerry"];
+1

:

 [Embed(source = "../art/Inventory/MegaBerry.png" , symbol="MegaBerry")]
 public static var MegaBerry:Class;
0

actionscript name, , .

, ,

var myBerry = new MegaBerry();

getDefinitionByName("myBerry") null.

, myBerry.name = "myBerry", getDefinitionByName("myBerry") , . .

, . assetName = new MegaBerry()?

0

If you want to know what the truly full name of your class really is, you can do the following:

trace(getQualifiedClassName(Assets.MegaBerry));

You can do this from within Assets.as, for example.

You can return this line to getDefinitionByName()and get a link to the class.

trace(getDefinitionByName(getQualifiedClassName(SomeClass)));
// output [class SomeClass]

And remember, getDefinitionByName()it only gets references for classes that are in the same scope as the getDefinitionByName call. Thus, if you download external SWFs, getting class references will depend on the application domain used and where this code is executed.

0
source

All Articles