Shahbaz Pothiawala
Shahbaz Pothiawala

Reputation: 1215

Refer to child of dynamically created movieclip actionscript 3

So this is how I add a movieclip dynamically

var ClassReference:Class = getDefinitionByName('some_name') as Class;
myBody = new ClassReference();
myBody.name = "curtheme"; // I am giving it an instance name here
addChild(myBody);

then I have many children of this dynamically added movie clip

var hair:Array = [curtheme.getChildByName('hair1'), curtheme.getChildByName('hair2'), curtheme.getChildByName('hair3'), curtheme.getChildByName('hair4'), curtheme.getChildByName('hair5'), curtheme.getChildByName('hair6'), curtheme.getChildByName('hair7'), curtheme.getChildByName('hair8')]; 

var top:Array = [curtheme.getChildByName('top1'), curtheme.getChildByName('top2'), curtheme.getChildByName('top3'), curtheme.getChildByName('top4'), curtheme.getChildByName('top5'), curtheme.getChildByName('top6'), curtheme.getChildByName('top7'), curtheme.getChildByName('top8')];

this gives error: Access of undefined property curtheme. Obviously this is wrong but then how do I access its children by name? I hope my question is straight forward.

Upvotes: 0

Views: 166

Answers (1)

3vilguy
3vilguy

Reputation: 981

What about:

var curtheme:DisplayObjectContainer = this.getChildByName('curtheme') as DisplayObjectContainer;

var hair:Array = [curtheme.getChildByName('hair1'), curtheme.getChildByName('hair2'), curtheme.getChildByName('hair3'), curtheme.getChildByName('hair4'), curtheme.getChildByName('hair5'), curtheme.getChildByName('hair6'), curtheme.getChildByName('hair7'), curtheme.getChildByName('hair8')];
var top:Array = [curtheme.getChildByName('top1'), curtheme.getChildByName('top2'), curtheme.getChildByName('top3'), curtheme.getChildByName('top4'), curtheme.getChildByName('top5'), curtheme.getChildByName('top6'), curtheme.getChildByName('top7'), curtheme.getChildByName('top8')];

or instead of: curtheme.getChildByName('hair1')

try

var hair:Array = [ this['curtheme']['hair1'], this['curtheme']['hair2'], this['curtheme']['hair3'], ...];

Upvotes: 1

Related Questions