Вы не можете добавить прослушиватель кликов в массив.
Глядя на свой код, вы перебираете внешний массив superVolumes
и пытаетесь добавить прослушиватель щелчков для его членов, но все его члены также являются массивами.
Что вы можете сделать, это вложенный цикл (цикл внутри цикла) и добавить слушателя к тому, что предположительно отображает объекты внутри этих подмассивов.
for (var i:int = 0; i < superVolumes.length; i++ ){
for(var j:int = 0; j < superVolumes[i].length; j++){
superVolumnes[i][j].addEventListener(MouseEvent.CLICK, openCabinet);
}
}
Чтобы определить, какому массиву принадлежит объект, по которому щелкают, вы можете сделать что-то вроде этого в обработчике щелчков:
//create a var to reference the clicked item's parent array
var arrayContainer:Array;
//a temporary variable to store the index of clicked object
var curIndex:int;
//loop through the superVolumes array
for (var i:int = 0; i < superVolumes.length; i++ ){
//see if the sub array contains the clicked item
curIndex = superVolumes[i].indexOf(e.currentTarget);
//indexOf returns -1 if the item is not found in the array
if(curIndex > -1){
//if the sub array contains the clicked item (e.currentTarget)
arrayContainer = superVolumes[i][curIndex];
break; //stop looping since you found the array
}
}
//now do whatever you need to do with the array
switch(arrayContainer){
case localSegment:
trace("You clicked an item from local segment");
break;
case external_MediaSegment:
trace("YOu clicked something from media segment");
break;
default:
trace("You clicked something from network_LocationSegment");
}
Что касается понимания того, что представляет [j]
выше, вы можете переписать его примерно так, чтобы было понятнее:
for (var iterator:int = 0; iterator < superVolumes.length; iterator++ ){
//the members of superVolumes are all arrays
//get the sub array at current index ('iterator') and assign it to a variable
var curArray:Array = superVolumes[iterator] as Array;
//now loop through this sub array
//since we have 'iterator' (previously 'i') as the iterator/index name in the outer loop
//we need a different name for the iterator on the inner loop
//let's call this iterator 'innerIterator' (previously 'j')
for(var innerIterator:int = 0; innerIterator < curArray.length; innerIterator++){
curArray[innerIterator].addEventListener(MouseEvent.CLICK, openCabinet);
}
}