PDA

View Full Version : removing an object from an array


vincentpol
03-05-2006, 01:15 PM
i am making a game where objects get generated and then the name is stored in an array, now lets say im shooting enemy2 which is on the 45th place in the array but i dont know that and i want to remove enemy2 from the array without leaving gaps in the array

so is there a way to remove an object from an array by the name of the content say
_root.enemies.remove("enemy2");
where enemies is the array

pls advise on what to do

llapgoch
03-05-2006, 01:26 PM
I'd do something like this - where allObjects is your complete array;


function remove(o:MovieClip){
for(a = 0; a < allObjects.length; a++){
if(allObjects[a] == o){
allObjects.splice(a, 1);
o.removeMovieClip();
break;
}
}
}


However, you'll need to pass the full movieclip name to the function.

vincentpol
03-05-2006, 01:28 PM
but wont that leave a gap in the array?

llapgoch
03-05-2006, 01:32 PM
No, the splice function won't leave a gap in the array.

vincentpol
03-05-2006, 01:46 PM
thx for the help:)