Forum Moderators: open
I'm not really very up on javascript, however, I have needed to create a multidimensional array of images which looks like this:
ar MultiArray = new Array(5)
MultiArray [1] = new Array(2)
MultiArray [1][1] = "gender-male.png"
MultiArray [1][2] = "gender-female.png"
MultiArray [2] = new Array(2)
MultiArray [2][1] = "hands-right.png"
MultiArray [2][2] = "hands-left.png"
MultiArray [3] = new Array(5)
MultiArray [3][1] = "height-1.png"
MultiArray [3][2] = "height-2.png"
MultiArray [3][3] = "height-3.png"
MultiArray [3][4] = "height-4.png"
MultiArray [3][5] = "height-5.png"
It works fine except for preloading. I have looked at scripts for preloading one dimensional arrays and have used what I thought was logical to create:
for (i=0; i < MultiArray.length; i++) {
for (j=0; j < MultiArray[i].length; j++) {
var preload = new Image();
preload.src = MultiArray[i][j];
}
}
which doesn't work!
Any thoughts?
var MultiArray = [
["gender-male.png", "gender-female.png"],
["hands-right.png", "hands-left.png"],
["height-1.png", "height-2.png", "height-3.png", "height-4.png", "height-5.png"]
];
Note, in general you should not use "new Array", but instead define the array as a literal using [].
As it goes, I needed to start the indexes from 1 because the page output is generated from php and the images all start with an index of '1' so reflecting this in the javascript made live much easier. I have just assigned nothing the first array entry:
["", "height-2.png", "height-3.png", "height-4.png", "height-5.png"]
As a point of interest, perhaps I could have started my "for" loop as i=1? Would that have worked?