-
Array question
I have a dynamic array. This is my code thus far.
Code:
add_student = function () {
var gender = _root.gender_text.text;
var Name = _root.name_text.text;
if(gender == "Male") {
var length = _root.boys.length;
_root.boys[length] = (Name);
//Is this not possible?
_root.boys[length][0] = "test";
_root.boys[length][1] = "test2";
//traces undefined
trace(_root.boys[length][1]);
}
}
I am trying to hurry and do this for a school project, but can't get the nested arrays to work. Anybody know what I am doing wrong?
-
try this:
_root.boys[length][0] = (Name);
_root.boys[length][1] = "test";
_root.boys[length][2] = "test2";
-
No that did not work, although I realize one of my mistakes.
What I want is to create a list of names.
Each of those names has proporties though. So for instance, I want this to happen.
array = [
["Joe", 1, 4, 3];
["Jim", 3, 2, 1]
]
Code change
Code:
add_student = function () {
var gender = _root.gender_text.text;
var Name = _root.name_text.text;
if(gender == "Male") {
var length = _root.boys.length;
_root.boys[length][0] = (Name);
_root.boys[length][1] = _root.choice_1.text;
_root.boys[length][2] = _root.choice_2.text;
trace(_root.boys[length][0]);
}
}
-
Try:
add_student = function () {
var gender = _root.gender_text.text;
var Name = _root.name_text.text;
if(gender == "Male") {
_root.boys.push([Name, _root.choice_1.text;, _root.choice_2.text;]);
trace(_root.boys[_root.boys.length-1]);
}
}
-
Did you init the array...
Code:
add_student = function () {
var gender = _root.gender_text.text;
var Name = _root.name_text.text;
if(gender == "Male") {
var length = _root.boys.length;
_root.boys[length] = [];
_root.boys[length][0] = "NAME";
_root.boys[length][1] = _root.choice_1.text;
_root.boys[length][2] = _root.choice_2.text;
trace(_root.boys[length][0]);
}
}
-
try something like this
Code:
if(gender == "Male"){
_root.boys.push(new Array(Name, _root.choice_1.text, _root.choice_2.text));
}
-
Thanks for the help. Kortex's method worked. Interestingly enough this works.
Code:
_root.boys[length].push([Name, _root.choice_1.text]);
_root.boys[length][2] = _root.choice_2.text;
.
Also, andreaskrenmair's way worked. This way makes the most sense. Johny, I didn't check out your method, but it looks like it would also work. Thanks again, for the help. The project is underway.