I am using flash.utils.getDefinitionByName in an attempt to grab an art asset. I use this function quite a bit and haven’t had trouble until now. Check it:
assetName = Assets.MegaBerry; // works
assetName = getDefinitionByName("Assets.MegaBerry") as Class; // doesn't work
What the heck?? Error response for the second line is “Variable not found.“
If it matters: Assets is a file in my root source directory (it has no package; Assets is the fully qualified name) and I’ve tried putting:
import Assets;
at the top with no luck.
For reference, in Assets.as I have:
[Embed(source = "../art/Inventory/MegaBerry.png")]
public static var MegaBerry:Class;
Your problem is that embedding the resource into the
Assetsclass will create a static variable of typeClassthat belongs to that class – which is what you are referencing when you useAssets.MegaBerry: A variable(!) of type Class.It does not, however, register the MegaBerry class to a fully qualified class name. To do this, you have to use – who would have guessed it –registerClassAliasat some point in your application:After that, it will be available everywhere else when calling
getDefinitionByName.** EDIT **
Well that’s some unexpected behavior… It turns out, the class that was embedded is in fact automatically registered, but under
{className}_{variableName}, instead of the notation you would expect. So using:should to the trick.
registerClassAliasalso works, but then you need to callgetClassByAliasinstead ofgetDefinitionByName. Sorry for the mix-up.** END EDIT **
You can also use the Embed tag to inject the resource into a separate class file, which you can then reference as expected by using
getDefinitionByName, or simply using an import: