1 Reply Latest reply on Oct 6, 2006 5:29 AM by AnandMX

    event handler functions and for loops

      This has always puzzled me. There may be some other way to approach this, but here's my question:

      Let's say I have 3 button instances with the names button1, button 2 and button3 that each affect a mc instance on the stage (mc1, mc2, and mc3). So when button1 is rolled over, it affects mc1, for example. I'd like to define functions that handle their onRollOver actions. So normally, if it were one button, I might do the following:

      button1.onRollOver = function() {

      instead of writing this script 3 times, however, I'd prefer to run through a for loop and automate the process:

      var buttons:Array = new Array(button1, button2, button3);

      for(i=1;i<4;i++) {
      buttons[ i ].onRollOver = function() {
      doSomethingTo(mc[ i ]);

      This works in the sense that it attaches the function to each of the 3 buttons. But the "i" variable within the created function doesn't increment, so all the buttons wind up affecting only one mc. I guess since the "i" variable within the function is not within the scope of the for loop it is not working. How do I do something like this? I'm sure I could be missing something obvious here, or this entire approach could be done some other way. Any insight would help, thanks.