Just a shot in the dark here... I could be completely wrong, I dont use 
closures much in AS. 

Does AS3 have block scope? (I suspect not). If not, then you're recycling the 
variable 'input' each time you loop. So each time you add a new event listener 
to 'input', you're adding it to all your TextInput instances. Just a guess - 
I've never tried it..


--- In flexcoders@yahoogroups.com, "Keith Hughitt" <keith.hugh...@...> wrote:
>
> Could someone please explain to me how closures work in ActionScript? I
> am attempting to create a set of Flex Form fields, and assign an
> event-handler to each of them, however, it seems that after creation,
> all of the fields have the same event handler.
> 
> For example:
> 
> var i:int = 0;
>            for each (var item:Object in this._myItems) {
>                  var f:FormItem = new FormItem();
>                  f.label = item.header;
> 
>                  var input:TextInput = new TextInput();
> 
>                  // Setup event-handler
>                  var self:MyClass = this;
> 
>                  input.addEventListener("change", function (e:Event):void
> {
>                      Alert.show("Event-hanlder #: " + String(i));
>                   });
> 
>                  i++;
> 
>                  // Add text input to FormItem
>                  f.addChild(input);
> 
>                  // Save a reference to the form control
>                  item.ui = f;
>                  addChild(f);
>              }
> 
> So I would expect that the number displayed when I change any given
> field matched the order of the field on the screen. When I run the code,
> however, no matter which field I adjust, the number is always the same
> (it is the number of the last form item to be added).
> 
> It appears that what is happening is that a single function is being
> created, and each input field has a reference to that same function.
> 
> Any suggestions? Any help would be greatly appreciated.
> 
> Thanks!
> Keith
>


Reply via email to