0

I would like to know if it is possible in actionscript3 to loop through a multidimensional array and add an event listener, and if it is, is my approach below right?

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();
}
0

1 Answer 1

3

You can't add a click listener to an Array.

Looking at your code, you loop through the outer array superVolumes, and attempt to add a click listener to it's members, but all it's members are also arrays.

What you can do, is a nested loop (a loop within the loop) and add the listener to what are presumably display objects inside those sub-arrays.

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);
    }
}

To determine which array the clicked object belongs to, you could do something like this in the click handler:

//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");
}

In regards to understanding what [j] above represents, you could rewrite it like so to make it clearer:

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);
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Okay, thanks would try that out. Nested loop is a new concept to me.
Sorry if it sounds silly, but what would [ j ] represent in this case?
You need to get an element from a 2D array. In AS3, 2D arrays are arrays of arrays. So, superVolumes[i] would net you an array which you can then iterate again, and superVolumes[i][j] nets you actual element of the current sub-array.
one more thing; please how can I use an if statement to detect what sub array from the superVolume array registered the click event; do I go.. function openCabinet (e:MouseEvent):void{ if (e.currentTarget == superVolumes[0]) { segment1.visible = false; } }
@SoloWalker You will need to iterate through superVolumes and check for superVolumes[i].indexOf(e.currentTarget) > -1 condition. If it is true, then current superVolumes[i] is the one that contains e.currentTarget.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.