1

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

4
  • 9
    What do you expect enemies_array[i, j] do? Did you mean enemies_array[i][j]? Commented Jul 14, 2015 at 11:59
  • Isn't that how we access multidimensional arrays? enemies_array[i][j] gives me an undefined error. Besides, all the other attributes are fine, it's just the Y that is being reset to 0. If I do enemies_array[i, j].x the values are fine, as with the rest of the attributes Commented Jul 14, 2015 at 12:04
  • 10
    No, it's not. enemies_array[i, j] is exactly equivalent to enemies_array[j] (see the comma operator). You'll have to change it everywhere, not only on the last line. Commented Jul 14, 2015 at 12:06
  • Thank you Juhana, that was it. Commented Jul 14, 2015 at 12:08

1 Answer 1

1

Here you 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);

    for (var i = rows - 1; i >= 0; i--) {
        enemies_array[i] = new Array(columns);
        for (var j = columns - 1; j >= 0; j--) {
            var x_position = j * (enemy_squadron_width / 4) + (ship_width / 2);
            var 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 (var i = rows - 1; i >= 0; i--) {
        for (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 to acces the elements of the array by a[i][j].

Fiddle: http://jsfiddle.net/bL4mwgez/1/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, Juhana already helped me earlier. I'll accept this answer nonetheless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.