0
votes

I used this line of code to remove an element:

tiles.splice(tiles.indexOf(tiles[i]), 1);

Yet afterwards when I'm checking the value, it's still not null, in fact, it still contains the movieclip it had inside of it.

This worked though:

tiles[tr] = null;

The question is, is it still okay to do it like that? I have movieclips added and removed to this array and I type removeChild(tiles[tr); before removing it from the array.

I just don't want to encounter some terrible performance in the future,

Thanks.

1
tiles.splice(i, 1) will work, no need to use indexOf, see if this resolves the issue. Also are you wanting to have the contents of the element (a movieclip for example) tagged for garbage collection or just to have it removed from the array?Simon McArdle
@Sim Just want it to be removed from the array, I won't need it any longerRadicate
Well splice(..) removes the element then shifts the remaining elements up one position, so if you splice the 2nd element in the array, the 3rd element will move up to take the position of the 2nd, the 4th will move to the 3rd position etc. This is maybe why your trace is still saying there is something at that position in the array.Simon McArdle

1 Answers

1
votes

splice cuts out the element from the Array, and all the above elements move one stept down. Which also reflects in the Array's length. So the element you are finding is the next element. Just nulling it will do just that. Leave the rest of the Array as is with an empty position.

Also you dont need to call indexOf if i is already the position you need.

tiles.splice(i, 1)

will do.