actionscript-3 - 将事件侦听器添加到多维数组
问题描述
我想知道是否可以在 actionscript3 中循环遍历多维数组并添加事件侦听器,如果可以,我的方法是否正确?
var localSegment:Array = [segment1.system_Cab, segment1.programs_Cab, segment1.userFiles_Cab, segment1.rollback_Cab]
var external_MediaSegment:Array = [segment2.externalHD_Cab, segment2.virtualDisk_Cab]
var network_LocationSegment:Array = [segment3.homeServer_Cab, segment3.wifiFlashdrive_Cab, segment3.encrivaPlay_Cab]
var superVolumes:Array = [localSegment, external_MediaSegment, network_LocationSegment]
for (var i:Number = 0; i < superVolumes.length; i++ ){
var fmCabinent = superVolumes[i];
fmCabinent.addEventListener(MouseEvent.CLICK, openCabinet);
}
var targetCabinent;
function openCabinet (e:MouseEvent):void{
targetCabinent = e.currentTarget;
if (targetCabinent.currentFrame == 1){
targetCabinent.play();
}
解决方案
您不能将点击侦听器添加到数组。
查看您的代码,您遍历外部数组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);
}
}
推荐阅读
- javascript - 如何在 Google Apps 脚本中拆分字符串以避免“JOIN 的文本结果超过 50000 个字符的限制”?
- javascript - 我可以找到 html 网页中的特定 div id 是否存在初始化变量吗?
- python - 在数据集中进行分割
- r - 变异一列并根据 matchin gof 两列从另一列填充其值
- vue.js - 存储状态更改时数据不更改
- java - 在 Java 中使用 MongoDB 驱动程序 POJO
- c# - Epplus 将范围对象转换为 c# 模型
- reactjs - RequireJs 与 Babel
- angular - 使用角度形式 ControlValueAccessor 和 mat-autocomplete
- spring-boot - 在 Spring Cloud Gateway 中将配置从 Ribbon 更改为 Spring Cloud Load Balancer