PDA

View Full Version : Multidimensional Arrays, 3-depth-problem



jns.johansson
November 16th, 2008, 09:42 AM
Hello, I wrote earlier about multi-arrays, I thought I understood, but it seems I really don't understand the display list. Why does this simple example not work?



sprites = new Array();
for (i = 0; i < 3; i++)
{
sprites[i].push(new Array());
sprites[i].name = "Level: 1";
for (j = 0; j < 3; j++)
{
sprites[i][j].push(new Array());
sprites[i][j].name = "Level: 2";
for (k = 0; k < 3; k++)
{
sprites[i][j][k].name = "Level: 3";
}
}
}


Output: TypeError: Error #1010: A term is undefined and has no properties.

Valaran
November 16th, 2008, 12:54 PM
// By doing this you will define sprites[i] as an array
// Would be accessed through sprites[i][x]
sprites[i].push(new Array());

// Here, you are trying to add a property to the array, that is not possible.
sprites[i].name = "Level: 1";


The closest thing you could do to what you want is to use objects within your arrays, like the following: (Note that it's quite ugly!)


sprites:Array = {name:"Level 0", sub:[]};
for (var i:uint = 0; i < 3; i++){
sprites.sub[i] = {name:"Level: 1", sub:[]};
for (var j:uint = 0; j < 3; j++){
sprites.sub[i].sub[j] = {name:"Level: 2", sub[]};
for (var k:uint = 0; k < 3; k++){
sprites.sub[i].sub[j].sub[k] = {name:"Level: 3"};
};
};
};
// Accessed through...
// sprites.name <--- Level 0
// sprites.sub[i].name <--- Level 1
// sprites.sub[i].sub[j].name <-- Level 2
// sprites.sub[i].sub[j].sub[k].name <--- Level 3


Far from optimal, but I think it would work.

Sage_of_Fire
November 16th, 2008, 02:03 PM
Actually, it appears that Arrays are designated dynamic, so you can add attributes on the fly. Your problem is that you're not initializing the array before you try to use it. You should do this:


sprites[i] = []; // same as new Array(), but quicker

You should also declare the name variable before you try to use it:


var name:String;

Once you handle those, it ought to work.

Valaran
November 16th, 2008, 02:13 PM
I just have to reply again :h: Are arrays really dynamic? :( I feel ashamed for not knowing, disregard my previous post please.

Sage_of_Fire
November 16th, 2008, 02:59 PM
Yep, you can see it in the language reference here (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html). public dynamic class Array. Very few classes, internal or external, use that attribute, but it's one of Array's best features. As my mom likes to say, "You learn something new every day."

jns.johansson
November 17th, 2008, 06:32 PM
Hey, thanks for the replies, I will try them now.

But Valaran, adding the name-attribute actually does work, but not when it's 3 levels deep, if I cut the last for-loop it outputs correctly.

Anyways, thanks!

jns.johansson
November 17th, 2008, 07:20 PM
1118: Implicit coercion of a value with static type Object to a possibly unrelated type Array. Is what I get when I try your example Valaran. But I will disregard of your post :D
Actually I have tried your way before Sage_of_Fire and ofcourse it's correct, I have more had the problem of reassigning arrays.

array[0] = ["dad"];
array[0][0] = ["child"];

well child replaces dad here, it takes a while for the head to understand that the last for loop is filling up the whole array (ofcourse it's logical that the new array replaces the previous value..). this is somewhat what I wanted to make, and I think it will work, thanks for the help


sprites = [];
for (i = 0; i < xml.length(); i++)
{
sprites[i] = [];
sprites[i].name = xml[i].@type;
for (j = 0; j < xml[i].word.length(); j++)
{
sprites[i][j] = [];
sprites[i][j].name = xml[i].word[j];
for (k = 0; k < 3; k++)
{
sprites[i][j][k] = [xml[i].word[j] + em[k]];
sprites[i][j][k].name = xml[i].word[j] + em[k];
}
}
}