I'm having a bit of a problem with this function:
function create_enemies(rows, columns) {
var i, j;
var x_position = 0;
var y_position = 0;
var id = 0;
enemies_array = new Array(rows);
for (i = rows - 1; i >= 0; i--) {
enemies_array[i] = new Array(columns);
for (j = columns - 1; j >= 0; j--) {
x_position = j * (enemy_squadron_width / 4) + (ship_width / 2);
y_position = i * (enemy_squadron_height / 4);
enemies_array[i, j] = {
x : x_position,
y : y_position,
width : ship_width,
height : ship_height,
speed : 2,
id : id
};
id++;
console.log("this one's fine: " + enemies_array[i, j].y);
}
}
for (i = rows - 1; i >= 0; i--) {
for (j = columns - 1; j >= 0; j--) {
console.log("This one's not fine: " + enemies_array[i, j].y);
}
}
}
What's happening is that on the first console.log, the Y attribute is being correctly printed, but on the second console.log, every Y in every element of the array is set at 0. Somehow the Y attribute is lost between the first outer for-loop and the second.
I'm surely missing something very obvious, and starting to feel a little insane.
Any ideas?
Thank you very much.
edit - I should mention that every other attribute is fine. Only the Y is being reset
enemies_array[i, j]do? Did you meanenemies_array[i][j]?enemies_array[i, j]is exactly equivalent toenemies_array[j](see the comma operator). You'll have to change it everywhere, not only on the last line.