We help IT Professionals succeed at work.

AS3 Reference a dynamically created MovieClip

slightlyoff asked
Last Modified: 2013-11-11
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++){
		var myMC:MovieClip = new MovieClip();
		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;
		var i = new Loader();
		i.load(new URLRequest(imgList[t]));
		initY = initY + space;

Open in new window

Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)




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.


Open in new window

Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.