# How can I add and delete movieClips when a button is click using for a loop

**URL:** https://forum.kirupa.com/t/how-can-i-add-and-delete-movieclips-when-a-button-is-click-using-for-a-loop/305095
**Category:** flash
**Created:** [February 19, 2010, 4:10am UTC](https://forum.kirupa.com/t/how-can-i-add-and-delete-movieclips-when-a-button-is-click-using-for-a-loop/305095 "2010-02-19T04:10:19Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![fs\_tigre](https://avatars.discourse-cdn.com/v4/letter/f/edb3f5/32.png) [@fs\_tigre](https://forum.kirupa.com/u/fs_tigre)
#### Post date: [February 19, 2010, 4:10am UTC](https://forum.kirupa.com/t/how-can-i-add-and-delete-movieclips-when-a-button-is-click-using-for-a-loop/305095/1 "2010-02-19T04:10:19Z")

</div>

Hi,

I have a for loop that adds 5 movieClips to the stage at a random position every time I click the enterBtn, what I want to do if the enterBtn is click again remove the movieClips previously generated and leave only the new ones on the stage.

Oh, the number “5” in my for loop will be dynamically created so it may be different if the enterBtn is click again (I just used 5 for simplicity).

> enterBtn.addEventListener(MouseEvent.CLICK, calculator,false,0,true);

function calculator(event:MouseEvent):void {

```
for (var i=0; i&lt;5; i++) {
    var partMc:PartMc = new PartMc();
    partMc.x=Math.random()*300;
    partMc.y=Math.random()*300;
    addChild(partMc);
}

```

}

Can some help me with this?

Thanks a lot!
