Question: Hey guys so I have an array of square Movie Clips they are all added to the random positions on the stage using a timer the only thing that I am having some trouble trying to figure out is how to check if one object in the array is overlapping another object in the array when the timer pushes more objects on the stage. They are all overlapping each other so I wanted to check if that statement is true and if they are overlapping then push them to a different position where there are not other objects in that array on the stage

Here is what I have so far my timer object how they are added to stage:

```
          //create new movie clip
          var newBox = new mcBox();
          //add to stage
          stage.addChild(newBox);
          //add to array 
          aBoxesArray.push(newBox);

        for (var i:uint = 0; i < aBoxesArray.length; i++) 
        {
            //add the boxes to sizes array
            aSizesArray.push(aBoxesArray[i].width);

            //add box as movieclip so frame lables can work and we can get current e target
            for each(newBox in aBoxesArray)
            {
                newBox.addEventListener(MouseEvent.CLICK, onBoxClicked, false, 0, true);
            }

        }

```

here is the function where I was going to process the logic in my ENTER\_FRAME event listener:

```
private function checkBoxesOverlapping():void 
    {
        for (var i:int = 0; i < aBoxesArray.length; i++)
        {

            var currentBox:mcBox = aBoxesArray[i];

            if (currentBox.hitTestObject())
            {
                trace("OVERLAPPING");
            }
        }

    }

```

Im just not too sure what to add in the hitTestObject or how to really go about doing this. If anyone can point to the right direction or even give me clues Ill be more than thankful! Thanks everyone!

Answer: 
**Another way:**
You can also achieve this by calculating distance of the objects. 

```
function distance(p1, p2) {
    var dist, dx, dy: Number;
    dx = p2.x - p1.x;
    dy = p2.y - p1.y;
    dist = Math.sqrt(dx * dx + dy * dy);
    //Obj boundaries test
    //Registraion point of the MovieClip must be center of the object.
    if (dist < (p1.width / 2 + p2.width / 2)) {
        //trace(p1.name, p2.name);
    }
}

```

overlap test

```
function overlapTest(): void {
    for (i = 0; i < objs.length; i++) {
        var target: MovieClip = MovieClip(objs[i]);
        for (var j: uint = 0; j < objs.length; j++) {
            if (objs[j] != target) {
                distance(target, objs[j]);
            }
        }
    }
}

```
Add another loop:

```
private function checkBoxesOverlapping():void 
{
    for (var i:int = 0; i < aBoxesArray.length; i++)
    {
        var currentBox:mcBox = aBoxesArray[i];

        for (var j:int = 0; j < aBoxesArray.length; j++)
        {
            var box:mcBox = aBoxesArray[j];

            if (currentBox != box && currentBox.hitTestObject(box))
            {
                trace("OVERLAPPING");
            }
        }
    }
}

```