For Loop - simple question

Sorry to bug you with such an easy one but its been two years for me since I’ve been away from Actionscript!

I’m using a For loop to program the buttons on my website. The first time I use i to associate the thumbNail functions it works well. However when I actually do click the button, the second i always refers to 12 - the final number in that loop.

How do I get the second *i *to correspond to the first i? I know its an easy fix, but I’m stumped!!! Thanks


for (i = 0; i < 12; i++) {
    mcBody.mainBody["thumbNail"+ i].onRelease    = function() {
        _root.mcBody.mainBody.textCaption.textDisp = thumbNailCaption*****;
        }
}

Yeah, this is kinda common - you’re loop assigns the onRelease to each clip, since the exist, but when the onRelease is triggered, it will only fire the last iteration.

to fix, this should work:


// I also datatyped and added a couple other "properisms"
for (var i:Number = 0; i < 12; i++) {
    var clip:MovieClip = mcBody.mainBody["thumbNail"+ i];
    clip.id = i;
    clip.onRelease = function() {
        _root.mcBody.mainBody.textCaption.textDisp = thumbNailCaption[this.id];
    }
}

there are much more proper ways to go about this - but, to get it working is the big thing. If you’re getting back into AS - look into things like the Delegate class in AS2 - its a nice thing to know of and understand prior to getting into as3 - hope this helps!

Yep, that’s exactly what I do, works perfectly :wink:

Thanks, that did the trick. Considering how different our code was, I’m surprised that mine was at all functional! :slight_smile: