# Removing movieClips from an Array

**URL:** https://forum.kirupa.com/t/removing-movieclips-from-an-array/309181
**Category:** flash
**Created:** [May 8, 2010, 11:54pm UTC](https://forum.kirupa.com/t/removing-movieclips-from-an-array/309181 "2010-05-08T23:54:25Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ahao](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@ahao](https://forum.kirupa.com/u/ahao)
#### Post date: [May 8, 2010, 11:54pm UTC](https://forum.kirupa.com/t/removing-movieclips-from-an-array/309181/1 "2010-05-08T23:54:25Z")

</div>

Hi, i’m making a mini-game (part of a larger game) where worms pop up randomly on the screen. You can click on a spraycan do remove them all at once.  
After that it should repeat itself. But during the second wave, when I press my spraycan, it doesn’t work anymore and it gives me this error:

"ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.  
at flash.display::DisplayObjectContainer/removeChild()  
at TerragotchiV2\_fla::MainTimeline/dropPesticide() "

code

```auto
body.addEventListener(Event.ENTER_FRAME,addWorm);
var wormNum:Number = 0;

function addWorm(e:Event):void {
    var randomWorm = Math.random();
    
    
        if (randomWorm < 0.07) {
            
        var badWorm:BadWorm = new BadWorm;
        badWorm.scaleX = 0.6 ; badWorm.scaleY = 0.6;
        badWorm.doubleClickEnabled = true;
        badWormArray.push(badWorm);
        badWormArray[wormNum].x = Math.random() * 400; 
        badWormArray[wormNum].y = Math.random() * 340 + 340;
        badWormArray[wormNum].addEventListener(MouseEvent.DOUBLE_CLICK,hit);
        
        addChildAt(badWorm,4);
        wormNum += 1;
        
        }

}

```

To remove I use this function:

```auto
 for (var wormNum in badWormArray) {
                            removeChild(badWormArray[wormNum]);
                        }
                        badWormArray.splice(0);
                        wormNum = 0;

```

Also I noticed, for exmaple if 4 worms appeared the first time, I spray and remove them, I get this error 4 times: “TypeError: Error #1010: A term is undefined and has no properties.  
at TerragotchiV2\_fla::MainTimeline/addWorm()” and after that he continues to create worms.

I know this has to do with empty indeces in the Array, but the .splice(0) should reset it right?

Help, I’m stuck for hours!
