PDA

View Full Version : Dynamic references in for loops.


limitedwave
08-05-2008, 02:38 PM
Greetings ~ I'm wondering what method people use to load things into multiple movie clips, that have the same name barring the trailing numeral. As in - how to address the clip on the left side of the = sign with [i] type syntax. For example:

function loadImage0(url:String):void {
imageLoader[i] = new Loader();
imageLoader[i].load(new URLRequest(url));
imageLoader[i].contentLoaderInfo.addEventListener(Event.COMPLETE , imageLoaded[i]);
}

If I run something like this I get errors like imageLoader[i] doesn't exist. I've seen this before trying to pump up clips with a loop and getting thrown off by what to do on the left side of the = sign. Thanks.

Any help would be appreciated, thanks!

limitedwave
08-05-2008, 02:42 PM
Another example:

function addListeners():void{
for(var i=0; i< 11; i++){
logo[i].addEventListener(MouseEvent.ROLL_OVER, onOver[i]);
logoOver[i].addEventListener(MouseEvent.ROLL_OUT, onRolledOut[i]);
}

yields these errors:
1120: Access of undefined property logo.

xxneon
08-05-2008, 02:55 PM
as long as the objects exist .. you would do something like this..

for(var i=0; i< 11; i++){
this["logo"+i].addEventListener(MouseEvent.ROLL_OVER, onOver[i]);
this["logo"+i].addEventListener(MouseEvent.ROLL_OUT, onRolledOut[i]);
}

where you would have objects on the timeline named logo1,logo2,logo3.. etc.. etc..

limitedwave
08-05-2008, 03:01 PM
Awesome! Thank you so much. I changed the code slightly, to access the function properly...it seems to work fine. Thanks again.

this["logo"+i].addEventListener(MouseEvent.ROLL_OVER, this["onOver"+i]);