Link to home
Start Free TrialLog in
Avatar of slightlyoff
slightlyoff

asked on

AS3 Reference a dynamically created MovieClip

My latest issue is:  how do I reference a dynamically created movie clip?

I've attached the code below.  The code creates movie clips on the stage starting in the upper left corner and each subsequent movieclip is placed 50 pixels lower.  Each movieclip contains an image and text and the number of movieclips is determined by the length of the xml file.

So now I'm to the point of actually creating the animation for my movieclips.  I'm wanting to have them scroll up.  To do this I need to reference the dynamically created movieclips.  I thought I would do this by using the name of the movieclip: myMC.name = "mc" + t;  - so that "mc0", "mc1" and so on would refer to the movieclips.  This is not so!

When i try to do a trace to find out if "mc0" has any information it it, i get an error.

I hope this explains my issue.  Any ideas?  Am I thinking about this the wrong way?

Thanks for your help!
for (t=0;t<n;t++){
		trace(eventList[t]);
		var myMC:MovieClip = new MovieClip();
		addChild(myMC);
		myMC.name = "mc" + t;
		myMC.x = 5;
		myMC.y = initY;
		
		theLabel = new TextField();
		//theLabel.text = String(eventList[t]);
		theLabel.text = myMC.name;
		theLabel.x = myMC.x + 30;
		theLabel.autoSize = TextFieldAutoSize.LEFT;
		
		var format:TextFormat = new TextFormat();
        format.font = "Verdana";
        format.color = 0xFF0000;
        format.size = 12;
        format.underline = true;
 
        theLabel.setTextFormat(format);	
		myMC.addChild(theLabel);
		
		var i = new Loader();
		i.load(new URLRequest(imgList[t]));
		
		myMC.addChild(i);
		
		
		initY = initY + space;
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of moagrius
moagrius
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slightlyoff
slightlyoff

ASKER

Thanks!
this works as long as the trace(getChildByName("mc0").x); is inside the function createObj.
Is there a way to access it outside of the function?  I get a null error when I try.

Thanks again!
It's a method, and must be called from the parent container...  so whatever you're adding the clip to must call it (shown below).

If you're calling getChildByName from another scope, it'll look in it's own display list for an MC with that name - it needs to look at the tree containing that object (so whatever object used the addChild method to add that clip to it's display list).

Hope that helps.

someMC.addChild(myMC);
trace(someMC.getChildByName("mc0").x);

Open in new window