Search code examples
flashactionscript-3actionscriptflash-cs4

Dynamic Object Initiation As3


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 stage but can very from time to time. How would I go about calling them dynamically. for example

var newObject = new ["DynamicObject"]();

??

Is this possible in As3 ??


Solution

  • 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