Javascript object losing attribute -
i'm having bit of problem 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); (i = rows - 1; >= 0; i--) { enemies_array[i] = new array(columns); (j = columns - 1; j >= 0; j--) { x_position = j * (enemy_squadron_width / 4) + (ship_width / 2); y_position = * (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); } } (i = rows - 1; >= 0; i--) { (j = columns - 1; j >= 0; j--) { console.log("this one's not fine: " + enemies_array[i, j].y); } } }
what's happening on first console.log, y attribute being correctly printed, on second console.log, every y in every element of array set @ 0. somehow y attribute lost between first outer for-loop , second.
i'm surely missing obvious, , starting feel little insane.
any ideas?
thank much.
edit - should mention every other attribute fine. y being reset
here go:
function create_enemies(rows, columns) { var x_position = 0; var y_position = 0; var enemy_squadron_width = 100; var enemy_squadron_height = 100; var ship_width = 100; var ship_height = 100; var id = 0; var enemies_array = new array(rows); (var = rows - 1; >= 0; i--) { enemies_array[i] = new array(columns); (var j = columns - 1; j >= 0; j--) { var x_position = j * (enemy_squadron_width / 4) + (ship_width / 2); var y_position = * (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); } } (var = rows - 1; >= 0; i--) { (var j = columns - 1; j >= 0; j--) { console.log("this one's not fine: " + enemies_array[i][j].y); } } } create_enemies(10,10);
you need acces elements of array a[i][j].
fiddle: http://jsfiddle.net/bl4mwgez/1/
Comments
Post a Comment