PDA

View Full Version : load listeners through for loop



UberPooner
March 18th, 2009, 05:04 AM
Hello, I was wondering how I would be able to add listeners to the stage using a for loop. currently my code looks like this, it is being called when the document opens.
var i:Number;
for(i=1;i<8;i++){
thumbselect.thumb"+i+".addEventListener(MouseEvent.CLICK, images"+i+");
}

That is not working. I tried quoting the whole thing, that did not work. I did trace it like this:
trace(' thumbselect.thumb"+i+".addEventListener(MouseEvent.CLICK, images"+i+")')

and the trace came out with the correct strings.

thanks for your input

m90
March 18th, 2009, 05:14 AM
You have to use the display list to do this. Say all your thumbs are in a thumbContainer-DisplayObject it'd work like this:



for (var i:uint = thumbContainer.numChildren; i > 0; i--){
thumbContainer.getChildAt(i-1).addEventListener(MouseEvent .CLICK, images);
}



You should then have your images-function listen to who dispatched the event and get the according image.

UberPooner
March 18th, 2009, 06:41 PM
Thanks a lot for the reply m90, that makes a lot more sense then what i had. I was wondering about the getChildAt(1)method, does that grab the instance name of the first movie clip in the container starting from upper left corner?

Anogar
March 18th, 2009, 06:46 PM
It's organized by depth. I'd recommend putting the objects you want into an array and then looping through the array.