主题:  这段代码能用循环语句实现吗?

浮尘

职务:普通成员
等级:3
金币:7.0
发贴:1258
#12004/4/13 23:53:01
三个按钮:btn0、btn1、btn2
三个串,在一个数组中:which
想用循环来写下面的事件处理函数,未成功。该怎么写?
btn0.onRelease=function(){
    txtInfo.text=which[0];
};
btn1.onRelease=function(){
    txtInfo.text=which[1];
};
btn2.onRelease=function(){
    txtInfo.text=which[2];
};



UndeadCraft

职务:版主
等级:4
金币:10.0
发贴:1993
#22004/4/14 8:56:55
var which:Array=["asdf","123","yvgfg"];
for (var i = 0; i<3; i++) {
    eval("btn"+i).num=i;
    eval("btn"+i).onRelease = function() {
        textinfo.text = _root.which[this.num];
        trace (_root.which[this.num]);
    };
}



浮尘

职务:普通成员
等级:3
金币:7.0
发贴:1258
#32004/4/14 11:19:16
谢谢!我原来是这样写的:
var which=new Array("A","B","C");
for(i=0;i<3;i++){
_root["btn"+i].onRelease=function(){
txtInfo.text=_root.which[i];
};
}
就是不起作用!
天堂兄给每个按钮指定一个参数num就可以了。
我只是猜测是由于变量 i 引起的,可不明白是怎么引起的。



dawnsky

职务:普通成员
等级:1
金币:0.0
发贴:1
#42004/4/16 17:33:16
万分感谢中....