PDA

View Full Version : Adding a child of a custom class?



benkyoto
October 1st, 2007, 05:47 AM
Hope there is someone here kind and knowledgable enough to help me out with this.

I have a gallery that has several predetermined categories, each on with a button.
The buttons are named with the "bt"+(gallery name) convention
So, i got instance names like "btcolor", "btsketch", "bttape". I subtract the first two letters and use the remaining substring to call the images in the gallery itself. The gallery images are named "color1", "color2",...
I want to place a custom embelm in the header of each gallery. They include an illlustration, so generating them via AS is not an option. I have them in my library and chose to output them to AS in the properties dialog, using the naming convention "em"+(gallery name).
Problem is I don't know how to add a child of a dynamically determined class name.

var simbolo:["em"+destino] = new ["em"+destino];

does not compile... I want the "simbolo" to be an instance of the class "em"+(class), depending on what the user clicks (ex: emcolor, emsketch...). They work if I reference them explicitly,

var simbolo:emcolor= new emcolor();

but how can I reference them dynamically?
Any ideas?

Here's the full code:


btcolord.addEventListener (MouseEvent.CLICK,opengallery);
bttrend.addEventListener (MouseEvent.CLICK,opengallery);
[...]

function opengallery(evt:Event):void
{
var destino:String=evt.target.name.substring(2,evt.tar get.name.length);
trace (destino);

var square:Sprite = new Sprite();
square.graphics.beginFill (0x000000,0.5);
square.graphics.drawRect(0,0, stage.stageWidth-100,stage.stageHeight-100);
square.graphics.endFill();
var punto:Point = new Point(50,50);
var punctum:Point = this.globalToLocal(punto);
square.x=punctum.x;
square.y=punctum.y;
addChild(square);

var simbolo:["em"+destino] = new ["em"+destino];
simbolo.x=punctum.x;
simbolo.y=punctum.y;
addChild(simbolo);
}

stop();

soulwire
October 1st, 2007, 06:19 AM
You use the getDefinitionByName methods:


var myClass:Class = getDefinitionByName("em"+destino) as Class;
var simbolo:Object = new myClass();

Then work from there! :)

benkyoto
October 1st, 2007, 06:25 AM
Worked like a charm. Thank you SO much!

It'll probably take me less than 7 minutes to get stuck in a different code dilemma, but o well.

Cheers!