# removeChild() Question

**URL:** <https://forum.kirupa.com/t/removechild-question/286611>\
**Category:** flash\
**Created:** [April 17, 2009, 6:01pm UTC](https://forum.kirupa.com/t/removechild-question/286611 "2009-04-17T18:01:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![madhat](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@madhat](https://forum.kirupa.com/u/madhat)\
**Post date:** [April 17, 2009, 6:01pm UTC](https://forum.kirupa.com/t/removechild-question/286611/1 "2009-04-17T18:01:43Z")

</div>

Hi Everyone,

I’m an AS2 programmer making the jump to AS3. I’m having trouble grasping the display set. Here’s what I’m trying to do:  
[LIST]  
[_]I have a button on the stage. When you press it, it duplicates an object from my library: redBall  
[_]The redBall has an event listener attached that moves the ball to the left side of the stage every frame tick.  
[\*]When the ball hits the left side of the stage, I want to remove it using removeChild. I can’t seem to access the event target to make that happen.  
[/LIST]Here’s the code:

```auto
 
//----------------------------------------------------------------------
 
[COLOR=#993300]var[/COLOR] numberBalls:[COLOR=#993300]Number[/COLOR] = [COLOR=#000000]0[/COLOR]; [COLOR=#f000f0]*//Var that counts the number of balls created*[/COLOR]
addBall_mc.[COLOR=#000000]addEventListener[/COLOR][COLOR=#000000]([/COLOR]MouseEvent.[COLOR=#000000]MOUSE_DOWN[/COLOR], launchBall[COLOR=#000000])[/COLOR]; [COLOR=#f000f0]*//When the button is presses, create a ball*[/COLOR]
 
[COLOR=#993300]var[/COLOR] ballContainer:Sprite = [COLOR=#993300]new[/COLOR] Sprite[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#f000f0]*//Create a container to hold the balls*[/COLOR]
addChildAt[COLOR=#000000]([/COLOR]ballContainer, [COLOR=#000000]0[/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#f000f0]*//Add the container to the bottom of the stage*[/COLOR]
 
[COLOR=#993300]function[/COLOR] launchBall[COLOR=#000000]([/COLOR]evt:MouseEvent[COLOR=#000000])[/COLOR]:[COLOR=#993300]void[/COLOR][COLOR=#000000]{[/COLOR] [COLOR=#f000f0]*//This function creates a new ball*[/COLOR]
    [COLOR=#993300]var[/COLOR] redBall:Sprite = [COLOR=#993300]new[/COLOR] Ball[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#f000f0]*//Create a new sprite*[/COLOR]
    redBall.[COLOR=#000000]x[/COLOR] = [COLOR=#000000]550[/COLOR]; [COLOR=#f000f0]*//move it to the right side of the stage*[/COLOR]
    redBall.[COLOR=#000000]y[/COLOR] = [COLOR=#993300]Math[/COLOR].[COLOR=#993300]random[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]*[COLOR=#000000]400[/COLOR]; [COLOR=#f000f0]*//Randomize the heigh of the new ball*[/COLOR]
    redBall.[COLOR=#993300]name[/COLOR] = [COLOR=#0000ff]"redBall"[/COLOR]+numberBalls; [COLOR=#f000f0]*//Give it a name*[/COLOR]
    ballContainer.[COLOR=#000000]addChild[/COLOR][COLOR=#000000]([/COLOR]redBall[COLOR=#000000])[/COLOR]; [COLOR=#f000f0]*//Add the new ball to the display list*[/COLOR]
    redBall.[COLOR=#000000]addEventListener[/COLOR][COLOR=#000000]([/COLOR]Event.[COLOR=#000000]ENTER_FRAME[/COLOR], moveLeft[COLOR=#000000])[/COLOR]; [COLOR=#f000f0]*//Attach an even listener to the new ball*[/COLOR]
    numberBalls++; [COLOR=#f000f0]*//increament ball counter*[/COLOR]
[COLOR=#000000]}[/COLOR]
 
[COLOR=#993300]function[/COLOR] moveLeft[COLOR=#000000]([/COLOR]evt:Event[COLOR=#000000])[/COLOR]:[COLOR=#993300]void[/COLOR][COLOR=#000000]{[/COLOR] [COLOR=#f000f0]*//Enterframe function that moves the ball left 3 pixels per frame*[/COLOR]
    evt.[COLOR=#993300]target[/COLOR].[COLOR=#000000]x[/COLOR] -= [COLOR=#000000]3[/COLOR];
    [COLOR=#993300]if[/COLOR][COLOR=#000000]([/COLOR]evt.[COLOR=#993300]target[/COLOR].[COLOR=#000000]x[/COLOR] <= [COLOR=#000000]30[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR] [COLOR=#f000f0]*//When the ball moves to the left side of the screen, remove the event listener, which stops the ball*[/COLOR]
        evt.[COLOR=#993300]target[/COLOR].[COLOR=#000000]removeEventListener[/COLOR][COLOR=#000000]([/COLOR]Event.[COLOR=#000000]ENTER_FRAME[/COLOR], moveLeft[COLOR=#000000])[/COLOR];
        [COLOR=#f000f0]*//*[/COLOR]
        [COLOR=#f000f0]*// But I want to remove the ball object from the stage?! *[/COLOR]
        [COLOR=#f000f0]*// How do I remove the event target using removeChild?*[/COLOR]
        [COLOR=#f000f0]*//*[/COLOR]
    [COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]
 
//----------------------------------------------------------------------

```

Any help would be greatly appreciated!  
Thanks,  
MadHat :top\_hat:
