查找属性的数组对象的索引

时间:2014-03-16 23:48:05

标签: actionscript-3 flash

以下是情况的简化版本:

//"mc" and "mc2" are Movie Clip instance names
var ary:Array=[mc, mc2];
mc.timer=new Timer(1000);
mc2.timer=new Timer(1000);
for(var i=0;i<ary.length;i++){
    ary[i].timer.addEventListener(TimerEvent.TIMER,alarm);
}
function alarm(e:TimerEvent){
    //how do I figure out which MovieClip's timer went off, as well as that mc's index?
}

2 个答案:

答案 0 :(得分:1)

嗯,我确定有人可以拿出更好的东西,但是我不知道......

传入事件处理程序方法的TimerEvent对象包含对Timer对象的引用(查找&#39; target&#39;属性)。从那里我想你可以循环然后手动检查它属于哪个movieclip ...但是,如果有很多它们,也许使用一个字典然后将你的计时器映射到动画片段?

一旦你获得了动画片段参考,你就可以找到它的父母并将其编入索引,等等。

(通常情况下,我不会将计时器参考放在movieclip -didn中,甚至会注意到movieclip类是动态的。)

答案 1 :(得分:1)

找到目标计时器

function alarm(e:TimerEvent){
    var timer:Timer = e.target as Timer;
}

找到目标mc

var ary:Array=[mc, mc2];
mc.timer=new Timer(1000);
mc2.timer=new Timer(1000);

//save the MovieClip
var dict:Dictionary = new Dictionary();
dict[mc.timer] = mc;
dict[mc2.timer] = mc2;

for(var i=0;i<ary.length;i++){
    ary[i].timer.addEventListener(TimerEvent.TIMER,alarm);
}

function alarm(e:TimerEvent){
    var timer:Timer = e.target as Timer;

    var mc:MovieClip =  dict[timer] as MovieClip;//get the mc
}