• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • Last Modified:

Using a variable in AddChild target in ActionScript 3

I have a collection of instances of a moveClip that already exist in my timeline named emptyThumb_mc0, emptyThumb_mc1, etc.

I'm using XML and a loop to load external images one at a time and I'm trying to cycle through and add them to each of the movieClips using AddChild.

AddChild works just fine if I hardcode the name of the target movieclip like this:
except of course, then all my images are loaded into that one MovieClip.

I want to use the variable from my loop to cycle through each target movieclip so I changed the code to this:

When I change the code to this, I get an 'Error#1010 A term is undefined and has no properties. I've traced the i value and it is has a correct value through each loop. So... is my syntax incorrect, or is just impossible to put a variable into the target path like this?

I've attached the relevant function code

//function that dynamically displays thumbnails
function xmlLoaded(event:Event):void
	//take data from XML import and put it into an array variable
	xml = XML(event.target.data);
	xmlList = xml.children();
	//loop that creates a new image loader for each item in the list of images
	for(var i:int = 0; i < xmlList.length(); i++)
		imageLoader = new Loader();
		//get the path to the thumbnail image file for each loader
		imageLoader.load(new URLRequest(xmlList[i].attribute("thumb")));
		//set position for the loader on the stage
		imageLoader.x = 0;
		imageLoader.y = 0;
		//collect path name to large image for each thumbnail and save as the name attribute
		imageLoader.name = xmlList[i].attribute("source");
		//add thumbnail to the stage
		//add listener to thumbnail to react to a mouse click to show full-size image
		imageLoader.addEventListener(MouseEvent.CLICK, showPicture);

Open in new window

1 Solution
try this:
this.thumbGrid_mc["emptyThumb_mc" + i].addChild(imageLoader);

Open in new window

beemerrider130hpAuthor Commented:
That did the trick, thank you so much!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now