# Pushing and Popping an Array

**URL:** <https://forum.kirupa.com/t/pushing-and-popping-an-array/258835>\
**Category:** flash\
**Created:** [April 28, 2008, 7:57pm UTC](https://forum.kirupa.com/t/pushing-and-popping-an-array/258835 "2008-04-28T19:57:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![RebuiltJorge](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@RebuiltJorge](https://forum.kirupa.com/u/RebuiltJorge)\
**Post date:** [April 28, 2008, 7:57pm UTC](https://forum.kirupa.com/t/pushing-and-popping-an-array/258835/1 "2008-04-28T19:57:01Z")

</div>

I have two buttons, one that adds a ball to an array and displays it and one pops a ball from the array and removes it from the display list, the add button works fine, however when i push the subtract button I get this message \*\*ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

\*\*Here is my code, thanks:

var iconArray:Array = new Array();  
var ball:Ball;  
var icon\_count:int = 0;

add\_btn.addEventListener(MouseEvent.CLICK,startAdd);  
sub\_btn.addEventListener(MouseEvent.CLICK,stopAdd);

function startAdd(evt:MouseEvent){

```
addBall(1);

```

}

function stopAdd(evt:MouseEvent){  
subBall(1);  
}

function subBall(num\_ball:int){  
add\_btn.removeEventListener(MouseEvent.CLICK,startAdd);

```
for(var i:int = 0; i &lt; num_ball; i++){
    iconArray.pop()
    removeChild(ball);
    trace(iconArray);
}

```

}

function addBall(num\_ball:int){

```
for(var i:int = 0; i &lt; num_ball; i++){
    ball = new Ball();
    iconArray.push(ball);
    trace(iconArray);
    addChild(ball);
    ball.x = icon_count * 100;
    ball.y = 100;
    icon_count++;
}

```

}
