Simple on.Release problem

myArray = [“homeText.txt”,“galleryText.txt”,
“linksText.txt”, “aboutmeText.txt”, “emailmeText.txt”];
for(i=0;i<5;i++){
button.duplicateMovieClip(“button”+i, i+1);
_root[“button”+i]._y += _root[“button”+(i-1)]._y - 10;
_root[“button”+i].onRelease = function(){
trace(myArray*);
}
}

I want button0 to trace myArray[0], and the same for the rest of the buttons.

I wrote seperate code for each button but i found that very tedious. Can someone help me fix this code?


  myArray = ["homeText.txt","galleryText.txt", "linksText.txt", "aboutmeText.txt", "emailmeText.txt"];
  for (i=0; i<5; i++) {
      var mc = button.duplicateMovieClip("button"+i, i+1);
      mc.id = i;
      mc._y += _root["button"+(i-1)]._y-10;
      mc.onRelease = function() {
          trace(myArray[this.id]);
      };
  }
  

We store the loop iterator as a property of the duplicated movieclip (to which a reference is stored in the variable mc to avoid the repeated associative array referencing), because when the button is being clicked, the loop has already finished and the loop iterator is lost. The loop sets the onRelease handler, but the actual click on the movieclip happens after the for loop has finished (unless you can click a movieclip faster than your processor can execute for loops ;)). So to fix this, we store the iterator as a property during the for loop so that it’s saved (mc.id), and we can then use it again in the onRelease handler :slight_smile:

Hey voetsjoeba, thanks a lot for the code. It works beautifully. I was thinking of doing it your way but couldnt implement the code.

On a side not, why do you type in the err…4th person(whichever the “we” perspective is).

You couldn’t implement it ? I just showed you how to implement it ! :stuck_out_tongue:

I don’t know why I talk in the We form … I always do that wehn explaining code, lol.

hhaaha no no…i meant before you showed me how to do it. My mind was on the same track but couldnt code it out.