Reputation: 15
I was looking for a solution to removing lists of movieclips from the stage in as3. I had a go at adding the movieclips to an array and making a loop that removes each of them if they are present. I had to include the if contains because it was sending me back this without it: "Error #2025: The supplied DisplayObject must be a child of the caller."
var array: Array = new Array;
var symbol1: MovieClip = new Symbol1;
var symbol2: MovieClip = new Symbol1;
array.push(symbol1);
array.push(symbol2);
stage.addChild(array[1]);
for (var i = 0; i < array.length; i++) {
if (contains(array[i])) {
stage.removeChild(array[i]);
trace("removed symbol[i]");
}
}
Am I using arrays wrong?
Upvotes: 1
Views: 638
Reputation: 14406
For more modular code (you can reuse no matter the parent), try doing it this way:
for (var i = 0; i < array.length; i++) {
if (array[i].parent) { //check to see if this item has a parent
array[i].parent.removeChild(array[i]); //tell the parent to remove this child
trace("removed symbol [i]");
}
}
This way, if you decide later that you'd like to have all your items in a container instead of stage, you don't have to change the code.
Upvotes: 0