1
votes

I'm working in Flash CS6 with Adobe AIR 3.3 and having trouble deleting movie clips after I have added them to the stage. Here's my code:

for(var i = 0; i<starCount; i++)
{
    star = new newStar();
    choiceBoard.addChild(star);
    star.x = 195;
    if(i>=1)
    {
        star.x = 195 + 90*i;
    }
    star.y = 597
}   

This is part of a function I've created that adds star shaped movie clips dynamically...It adds a movie clip, adds space between, adds the next movie clip etc. How can I delete all the star movie clips after they are created?

I've tried:

for(var i = 0; i<starCount; i++)
{
    if(contains(star))
    {
        choiceBoard.removeChild(star);
        trace("removed star");
    }
}

But this only deletes one star, regardless what "starCount" is set to. Thanks in advance.

3

3 Answers

2
votes
private var stars:Array=[];
for(var i = 0; i<starCount; i++)
{
    star = new newStar();
    stars.push(star);
    choiceBoard.addChild(star);
    star.x = 195 + 90*i;
    star.y = 597
}   

then try

for(var i = 0; i<stars.length; i++)
{
    choiceBoard.removeChild(stars[i]);
    trace("removed star");
}
0
votes

Are the stars they only children of choiceBoard? If so, you can just remove all children like so:

choiceBoard.removeChildren();

Alternatively, you can add all the stars to an array and then iterate through the array to remove them:

var starArray:Array = [];

//...

for(var i = 0; i<starCount; i++)
{
    star = new newStar();
    //put the star into the array
    starArray.push(star);
    choiceBoard.addChild(star);
    star.x = 195;
    if(i>=1)
    {
        star.x = 195 + 90*i;
    }
    star.y = 597
} 

And then to remove:

for(var i = 0; i<starArray.length; i++)
{        
    //remove the star from the display
    choiceBoard.removeChild(starArray[i]);      
    trace("removed star");
}
//clear the array
starArray = [];
0
votes

If you have added all your stars into one container (looks like you have with choiceBoard), and there is nothing else in that container that you want to keep around, then you can remove them very simply (and quickly) by doing this:

while(choiceBoard.numChildren)
{
    choiceBoard.removeChildAt(0);
}