Dynamic Object Initiation As3

前端 未结 6 1959
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 04:33

I notice in older version of flash you can create an instance of a dynamic class. I am creating a game that will have many different classes that can be displayed on the sta

6条回答
  •  余生分开走
    2021-01-17 04:49

    I think there are 2 ways you can do that:

    1.Using ApplicationDomain.getDefinition('DynamicTemplate')

    something like:

    var DynamicClass:Class = this.loaderInfo.applicationDomain.getDefinition('DynamicTemplate') as Class;
    addChild(new DynamicClass);
    

    You would need to do this when you file has INITialized.

    2.Using getDefinitionByName() :

    var DynamicClass:Class = flash.utils.getDefinitionByName('DynamicTemplate') as Class;
    addChild(new DynamicClass);
    

    If you need a way to get Class names to create new instances of objects, you could use describeType() or go through the instance's constructor, but I reckon you would know your classes anyway.

    var TemplateObj:Class = flash.utils.getDefinitionByName(describeType(yourIntance).@name) as Class;
    var newObj = new TemplateObj();
    var newObj2 = new yourIntance.constructor();
    

    Hope this help, George

提交回复
热议问题