Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Objects to a 2d Array

Status
Not open for further replies.

Terwin

Programmer
May 23, 2002
51
US
In my actionscript, I map parameters from an XML file to objects, and then add them to an array called tiles.

This was accomplished by using the push function at the end of each loop in the parsing routine.

However, I've reorganized the objects into a 2d array, and I'm unsure how to use the push function with it -- or if it's even possible at all. Setting the array[j] equal to the object adds the object properly, but the objects don't seem to retain all of their properties when I add them in this way. Any help would be appreciated.. code below:

Map.prototype.initialize = function(mapXML) {

mapXML.ignoreWhite = true; // Ignore Whitespace

var rootTag; // Root XML Element
rootTag = mapXML.firstChild;

// Obtain Root Element attributes, store in map object
this.rows = rootTag.attributes["rows"];
this.cols = rootTag.attributes["cols"];
this.numTiles = rootTag.attributes["numTiles"];

// For each Hex node
for (i = 0; i < (rootTag.childNodes.length); i++) {
// if node is valid (not null)
if (rootTag.childNodes.nodeName != null) {
// Declare Hex object this node represents
tempHex = new Hex(); // Temporary Hex Object
// Get Hex Attributes from current node
tempHex.row = rootTag.childNodes.attributes[&quot;row&quot;];
tempHex.col = rootTag.childNodes.attributes[&quot;col&quot;];
// For each Child of a Hex Node
for (j = 0; j < (rootTag.childNodes.childNodes.length); j++) {
// if node is valid (not null)
if (rootTag.childNodes.childNodes[j].nodeName != null) {
// Set Hex Properties based on node values
if (rootTag.childNodes.childNodes[j].nodeName == &quot;terrain&quot;) {
tempHex.terrain = rootTag.childNodes.childNodes[j].childNodes[0].nodeValue;
}
if (rootTag.childNodes.childNodes[j].nodeName == &quot;province&quot;) {
tempHex.province = rootTag.childNodes.childNodes[j].childNodes[0].nodeValue;
}
if (rootTag.childNodes.childNodes[j].nodeName == &quot;color&quot;) {
tempHex.colorStyle = rootTag.childNodes.childNodes[j].childNodes[0].nodeValue;
}

}// If != null
}// For each Hex Node Child

this.tiles[tempHex.col][tempHex.row] = tempHex;// Add tempHex to the Map.tiles array
trace(this.tiles[tempHex.col][tempHex.row].terrain);// This parameter shows undefined, even though tempHex.terrain shows the proper value.
}// If != null
}// For each Hex Node
};// Map.initialize()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top