# Loop addChild then target to add to top of DisplayList

**URL:** <https://forum.kirupa.com/t/loop-addchild-then-target-to-add-to-top-of-displaylist/292757>\
**Category:** Uncategorized\
**Created:** [July 18, 2009, 2:09pm UTC](https://forum.kirupa.com/t/loop-addchild-then-target-to-add-to-top-of-displaylist/292757 "2009-07-18T14:09:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanimations](https://avatars.discourse-cdn.com/v4/letter/v/b77776/32.png) [@vanimations](https://forum.kirupa.com/u/vanimations)\
**Post date:** [July 18, 2009, 2:09pm UTC](https://forum.kirupa.com/t/loop-addchild-then-target-to-add-to-top-of-displaylist/292757/1 "2009-07-18T14:09:52Z")

</div>

I am just trying to grasp a simple concept of utilizing loops, addChild, and eventListeners. My goal is to use a loop to create multiple instances of the same type of object from my library (called Ball), and when one is clicked, have it move “above” all other instances on the stage (and eventually drag, but that’s easy enough to make happen later). My current code attempt is:

for(var i:uint = 0; i\<=10;i++)  
{  
var my\_ball:Ball = new Ball() //new instances of Ball from library export  
addChild(my\_ball) //place ball in displayList  
my\_ball.name = “ball\_”+i+"\_mc" //give unique instance name to each ball  
my\_ball.x= (my\_ball.width\*.9)\*i //spread balls across screen with slight overlap  
my\_ball.addEventListener(MouseEvent.CLICK, add\_ball\_top\_of\_list)  
}  
function add\_ball\_top\_of\_list(event:MouseEvent):void  
{  
//\*\ ***WHAT SHOULD GO HERE?**

```
addChild(event.target.name)

//i want the clicked instance to be added to top of display list but what i have
//obviously returns the clicked instance's NAME, which is NOT what i need...
//i need to addChild(my_ball) NOT addChild(ball_#_mc)

//also, do i need to remove the targeted child or will adding it
//a second time automatically cause the "original" to be removed?

```

}

thanks in advance for any guidance
