0

Ok I have a simple for loop in Javascript that will create an array of numbers, I need to do this so I can then swap them around to create random positions. But anyways here is the code

            var aliveMonsters = [];
    var deadMonsters = 0;
    for( var i = 0; i < monsterAmount; i++ )
    {
        if( monsters[i].hp > 0 )
        {
            var place = i - deadMonsters;
            var placed = i - deadMonsters;
            aliveMonsters[place] = placed;
        }
        else
        {
            deadMonsters -= 1;
        }
        //console.log(i);
    }
            console.log(aliveMonsters);

when all 3 monsters are alive this is printed out

[0, 1, 2] 

Which is correct, but when one of them dies (0, 1 or even 2), the array then becomes this

[0, 3: 3] 

this code is called everytime a player makes a move and it works fine until a monster is dead (HP is set to 0). Can anyone see why this is happening?

Here is a picture, dont worry about the array that displays undefined. enter image description here

4

1 回答 1

0

Put deadMonsters += 1 纠正了错误

于 2013-09-01T22:17:06.063 回答