views:

25

answers:

1

I am pretty sure that this is totally what i need, however I can not get it to work for some reason. What i would like to do is call an arbitrary component that extends Canvas. Since there may be a variety of components named TestCanvasA, TestCanvasC, TestCanvasC which i won't know till run-time I figured this would be the way to go about it. Here is what i have.

<mx:Script>
    <![CDATA[
        import component.TestCanvas;
        import mx.containers.Canvas;
        import flash.display.Sprite;
        import flash.utils.getDefinitionByName;

        private function init(evt:Event):void{
            var Type:String="TestCanvas";
            var controlClass:Class = getDefinitionByName(Type) as Class;
            this.addChild(new controlClass() as Canvas);
        }
    ]]>
</mx:Script>

Any ideas would be awesome!

+1  A: 

Give it the fully qualified class name:

var type:String="component.TestCanvas";
var controlClass:Class = getDefinitionByName(Type) as Class;

Also a mere import statement need not include the definition of the class in the compiled SWF unless there are references to the class inside the application. Just declare (need not initialize) a variable of that type somewhere in the SWF to make sure that the definition is indeed included.

var dummy:TestCanvas;
Amarghosh
Thanks for getting back to me, but unfortunately that didn't work. To give you a better idea of what i am trying to do. I would like to create an external library for someone to add arbitrary template files which consist of mxml files. The way I envision it is that I won't know the name of the mxml file aside from it inheriting from Canvas. I will then load a config file during runtime which identifies the name of the mxml "templates" and do an addChild to add it to the stage. Let me know if that makes sense, or if i am doing something wrong. Thanks! I really appreciate it!
abritez