Results 1 to 5 of 5

Thread: Accessing element in multidimensional array

  1. #1

    Accessing element in multidimensional array

    I cannot wrap my head around this simple concept. All I need to do is access the element in myArray at the third position on Mouse Over. I've tried myArray[i][2] but it come back as the last element in the array. Help is appreciated.

    Here is my code.

    Code:
     
    function xmlLoaded(event:Event):void {
       myXML = XML(myLoader.data);
       trace("Data loaded.");
       for each (var property:XML in myXML.item) {
           var id:Number = Number(property.attribute("id"));
           var s:String = String(property.stateName);
           var f:String = String(property.formNum);
           myArray.push([id, s, f]);
      }
        colorStates();
    }
     
    function colorStates():void {
        var a:ColorTransform = new ColorTransform();
        for (var i:Number = 0; i<myArray.length; i++) {
     
    this[myArray[i][1]].addEventListener(MouseEvent.MOUSE_OVER, manageMouseOver);
     
    this[myArray[i][1]].addEventListener(MouseEvent.MOUSE_OUT, manageMouseOut);
     
      if (myArray[i][2] == "NA") {
         a.color = (0xb3b3b3);
         this[myArray[i][1]].transform.colorTransform = a;
      } else {
          a.color = (0x1883c7);
          this[myArray[i][1]].transform.colorTransform = a;
      }
     
      function manageMouseOver(e:MouseEvent):void {
          trace("Over " + e.target.name);
         Here -- > label.htmlText = "<b>"+ e.target.name +"</b><br>"+ myArray[0][2]; <--- Here
      }
     
      function manageMouseOut(e:MouseEvent):void {
          trace("Out");
          label.htmlText = "";
       }
     }
    }

  2. #2
    I think you will need to identify the element from myArray which is associated to the current mouseOver. With e.target you could iterate throught myArray with j lets say, check if this[myArray[j][1]] is equal to e.target and if so you have made an association. So for that j you will have to do myArray[j][2] to access the 3rd element of the j-th element.

    If you will try i, you will actually using the last iterated value for i because it is still visible from manageMouseOver function.

  3. #3
    Thank you for the help. Would you mind posting an example though? I'm not sure I follow exactly what you are saying.

    Thanks

  4. #4
    Figured it out. For anyone that wants to know.

    function manageMouseOver(e:MouseEvent):void {
    trace(id);
    pattern = new RegExp( "_", " " );
    var str:String = e.target.name;
    str = str.replace( pattern, " " );
    for (var j:Number = 0; j<myArray.length; j++) {
    if(e.target.name == myArray[j][1]) {
    label.htmlText = "<b>"+ str +"</b><br>"+ myArray[j][2];
    }
    }
    }

  5. #5
    Quote Originally Posted by Reean917 View Post
    Figured it out. For anyone that wants to know.

    function manageMouseOver(e:MouseEvent):void {
    trace(id);
    pattern = new RegExp( "_", " " );
    var str:String = e.target.name;
    str = str.replace( pattern, " " );
    for (var j:Number = 0; j<myArray.length; j++) {
    if(e.target.name == myArray[j][1]) {
    label.htmlText = "<b>"+ str +"</b><br>"+ myArray[j][2];
    }
    }
    }
    Well, that's the brute force approach. At every click, all possibilities will be tried until the correct item is found.

    Ideally, you should store the form number - state object relation during the objects constructions, so the accessing time is always constant. Oh, and don't nest functions when not strictly required.

    Code:
     
    var formNumByObjectName:Object = {} // creates the relation object
    
    function xmlLoaded(event:Event):void {
       myXML = XML(myLoader.data);
       trace("Data loaded.");
       for each (var property:XML in myXML.item) {
           var id:Number = Number(property.attribute("id"));
           var s:String = String(property.stateName);
           var f:String = String(property.formNum);
           myArray.push([id, s, f]);
      }
        colorStates();
    }
     
    function colorStates():void {
        var a:ColorTransform = new ColorTransform();
        for (var i:Number = 0; i<myArray.length; i++) {
     
    this[myArray[i][1]].addEventListener(MouseEvent.MOUSE_OVER, manageMouseOver);
     
    this[myArray[i][1]].addEventListener(MouseEvent.MOUSE_OUT, manageMouseOut);
     
      if (myArray[i][2] == "NA") {
         a.color = (0xb3b3b3);
         this[myArray[i][1]].transform.colorTransform = a;
      } else {
          a.color = (0x1883c7);
          this[myArray[i][1]].transform.colorTransform = a;
      }
    
      formNumByObjectName[myArray[i][1]] = myArray[i][2] // stores the required values
     
     }
    }
    
      function manageMouseOver(e:MouseEvent):void {
          trace("Over " + e.target.name);
         label.htmlText = "<b>"+ e.target.name +"</b><br>"+ formNumByObjectName[e.target.name];
      }
     
      function manageMouseOut(e:MouseEvent):void {
          trace("Out");
          label.htmlText = "";
       }
    Last edited by BoppreH; March 2nd, 2010 at 02:09 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012