Results 1 to 5 of 5
-
March 2nd, 2010, 08:47 AM #121Registered User
postsAccessing 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 = ""; } } }
-
March 2nd, 2010, 09:09 AM #211Claude Bur
postsI 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.
-
March 2nd, 2010, 09:29 AM #321Registered User
postsThank you for the help. Would you mind posting an example though? I'm not sure I follow exactly what you are saying.
Thanks
-
March 2nd, 2010, 11:27 AM #421Registered User
postsFigured 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];
}
}
}
-
March 2nd, 2010, 01:06 PM #5
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 01:09 PM.

Reply With Quote


Bookmarks