1 Reply Latest reply on Sep 18, 2008 10:12 AM by Gregory Lafrance

    Closure Problem

    _LJ
      Hi,

      I have a question about actionscript's function closure. In my Flex 2 template, i have a function who contains a for loop, for example:

      function blah():void {
      for(var i=0; i<3; ++i) {
      var label:Text = new Text();
      label.text = i;
      var box:VBox = new VBox();

      box.addEventListener("mouseOver",
      function(evt:Event):void {
      box.setStyle("borderThickness", 2);
      box.setStyle("backgroundColor", "#FFFFFF");
      box.setStyle("borderColor", "#000000");
      }
      );

      //blah... to add box to display
      }
      }


      Here the problem is, suppose i have created 3 boxes, each time I move mouse over a box, only the third one gets changed. I use "var" to create "new" memory each time for a VBox, so why this happen?


      Thanks in advance!