简体   繁体   中英

AS3 Removing an object from an array?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM