Last iteration of a loop - buttons

i have this from as2, where i can create many buttons on stage and loop. then create a function that applys to all the buttons


[COLOR=#993300]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#993300]var[/COLOR] i = [COLOR=#000000]1[/COLOR]; i <= [COLOR=#000000]3[/COLOR]; i++[COLOR=#000000])[/COLOR]
[COLOR=#000000]{[/COLOR]
    bt = [COLOR=#993300]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#0000FF]"button"[/COLOR] + i[COLOR=#000000]][/COLOR];
    
    bt.[COLOR=#000000]num[/COLOR] = i;
    bt.[COLOR=#993300]onRelease[/COLOR] = [COLOR=#993300]function[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR]
        [COLOR=#F000F0]*//trace(i); // would trace 4 for each button*[/COLOR]
        [COLOR=#F000F0]*//trace(this.num); // the value of i when the onRelease was created*[/COLOR]
        [COLOR=#993300]Text[/COLOR].[COLOR=#993300]text[/COLOR] = [COLOR=#993300]this[/COLOR].[COLOR=#000000]num[/COLOR];
    [COLOR=#000000]}[/COLOR]
    
[COLOR=#000000]}[/COLOR]

when i try it in as3, i get an undefined for thebutton that i pressed, can anyone help



[COLOR=#993300]var[/COLOR] aButtons:[COLOR=#993300]Array[/COLOR] = [COLOR=#993300]new[/COLOR] [COLOR=#993300]Array[/COLOR];
aButtons = [COLOR=#000000][[/COLOR]btn1, btn2, btn3[COLOR=#000000]][/COLOR];

[COLOR=#993300]for[/COLOR][COLOR=#000000]([/COLOR][COLOR=#993300]var[/COLOR] i:[COLOR=#993300]Number[/COLOR] = [COLOR=#000000]0[/COLOR]; i < aButtons.[COLOR=#993300]length[/COLOR]; i++[COLOR=#000000])[/COLOR]
[COLOR=#000000]{[/COLOR]
   aButtons[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR].[COLOR=#000000]buttonMode[/COLOR] = [COLOR=#993300]true[/COLOR];
   aButtons[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR].[COLOR=#000000]addEventListener[/COLOR][COLOR=#000000]([/COLOR]MouseEvent.[COLOR=#000000]CLICK[/COLOR], handleClick[COLOR=#000000])[/COLOR];
   
   [COLOR=#993300]function[/COLOR] handleClick[COLOR=#000000]([/COLOR][COLOR=#993300]e[/COLOR]:MouseEvent[COLOR=#000000])[/COLOR]:[COLOR=#993300]void[/COLOR]
    [COLOR=#000000]{[/COLOR]
         [COLOR=#993300]trace[/COLOR][COLOR=#000000]([/COLOR]aButtons[COLOR=#000000][[/COLOR]i[COLOR=#000000]][/COLOR][COLOR=#000000])[/COLOR];
    [COLOR=#000000]}[/COLOR]
    
[COLOR=#000000]}[/COLOR]