flash as3创建多维数组的最佳方法

时间:2011-05-13 20:21:15

标签: arrays flash actionscript-3

我有一系列电影片段(代表乐队成员),它们具有各种属性,其中有一个属性,告诉乐队成员离开当前乐队后去哪里。对于那些组建新组的人,我想创建一个数组。在该数组中,我想将所有留给同一组的组分组到辅助数组中。所以,如果你有五个乐队成员,其中两个留给X组,三个留给Y组。最好的方法是什么?这大致是我的代码:

var newGroupArr:Array = new Array() //this will hold all members leaving for a new group

for (k=0;k<memberClips.length;k++){
    if (memberClips[k].outcome == "new"){
        //for all groups where memberClips[k].group is the same, create a new array within newGroupArr for those similar members.
    }
}

或者我想如果我没有多维数组并且只是循环遍历所有成员并且说 - 对于那些组相同的成员,执行此函数,同一组的名称作为参数传递给功能。我想我遇到的麻烦就是确定谁是一样的。

This illustration shows the problem I'm having - if John Wetton is clicked, a line should be drawn only for him. But instead, a line is drawn for both he, david cross, and bill bruford, because they are all leaving for a new group. But david cross and bill bruford are actually going to a different group than john, so I need to make sure they are stored as members leaving for a new group, but I also need them grouped by the new band they are leaving for.

2 个答案:

答案 0 :(得分:1)

除非你需要使用数组的原因,否则我会使用一个字典,就像这样

var bands:Dicionary = new Dictionary();

for (k=0;k<memberClips.length;k++){
    if(memberClips[k].outcome=="new"){
        var newGroup:String = memberClips[k].group;
        if(!bands[newGroup]){
           bands[newGroup] = new Array();
        }
        bands[newGroup].push(k);
    }
}

现在乐队中的每个数组都将包含离开其前一个乐队的成员

答案 1 :(得分:0)