Public static getDefinitionByName from other Class


(Nirvan) #1

I searching for some time how to use getDefinitionByName and can’t solve my problem. I have class “GC” where I have embed sounds as public static const S_HIT0, s_HIT1 etc.

var ClassReference:Class = getDefinitionByName(“GC.S_HIT0”) as Class;

not working, it returns S_HIT0 instead of GC.S_HIT0. How can I do it correctly?


(Jacob Albano) #2

Does the resulting class not work as expected? When you say it returns S_HIT0, is that what you get when you trace it out?

[class S_HIT0]

That seems like the correct behavior to me.


(Nirvan) #3

But class what I want is in other “GC” class :stuck_out_tongue:

ReferenceError: Error #1065: Variable S_HIT0 is not defined.


(Jacob Albano) #4

So, when you say “it returns S_HIT0 instead of GC.S_HIT0”, what you actually mean is you get the reference error shown above.

I did some testing and was not able to get your approach working. In fact, getDefinitionByName will never work at all in this case, since for all we know the actual class is named something like FLX_SOUND_2937104729308271048385618. You’re not declaring a class called “GC.S_HIT0”, you’re telling the compiler “generate a class based on this asset, and then give me a reference to it”.

If what you’re trying to accomplish is to access the static variables of your GC class by name, you can do that by making the embedded assets public and using this:

var classReference:Class = GC["S_HIT0"];

Or, as it may be:

var allMyAssets:Array = ["S_HIT0", "S_HIT1", "S_HIT2"];
for each (asset in allMyAssets)
{
    var classReference:Class = GC[asset];
}