# hitTest on Object within the same array

**URL:** <https://forum.kirupa.com/t/hittest-on-object-within-the-same-array/260658>\
**Category:** flash\
**Created:** [May 18, 2008, 11:35am UTC](https://forum.kirupa.com/t/hittest-on-object-within-the-same-array/260658 "2008-05-18T11:35:16Z")\
**Posts on this page:** 6\
**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:** [May 18, 2008, 11:35am UTC](https://forum.kirupa.com/t/hittest-on-object-within-the-same-array/260658/1 "2008-05-18T11:35:16Z")

</div>

I have enemy ships appear four at a time, however sometimes they appear on top of each other, how can I make a hitTest that will determine if objects within the same array are hitting each other, this is the way I do my hitTest:

```
ActionScript Code:
[LEFT][COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] enemyNum:[COLOR=#0000FF]int[/COLOR]=enemies.[COLOR=#0000FF]length[/COLOR] - [COLOR=#000080]1[/COLOR]; enemyNum &gt;= [COLOR=#000080]0[/COLOR]; enemyNum--[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
            [COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] laserNum:[COLOR=#0000FF]int[/COLOR]=lasers.[COLOR=#0000FF]length[/COLOR] - [COLOR=#000080]1[/COLOR]; laserNum &gt;= [COLOR=#000080]0[/COLOR]; laserNum--[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
                [COLOR=#0000FF]if[/COLOR] [COLOR=#000000]([/COLOR]enemies[COLOR=#000000][[/COLOR]enemyNum[COLOR=#000000]][/COLOR].[COLOR=#000080]hitTestObject[/COLOR][COLOR=#000000]([/COLOR]lasers[COLOR=#000000][[/COLOR]laserNum[COLOR=#000000]][/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]

```

[/LEFT]

however this doesn’t work because the objects will end up hitting themselves

---

<div class="post-metadata">

**Author:** ![Exodar](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@Exodar](https://forum.kirupa.com/u/Exodar)\
**Post date:** [May 18, 2008, 2:09pm UTC](https://forum.kirupa.com/t/hittest-on-object-within-the-same-array/260658/2 "2008-05-18T14:09:23Z")

</div>

> [AS]for (var enemyNum:int=enemies.length - 1; enemyNum \>= 0; enemyNum–) {  
> for (var laserNum:int=lasers.length - 1; laserNum \>= 0; laserNum–) {  
> if (enemies[enemyNum].hitTestObject(lasers[laserNum])) {[/AS]

what do you mean the objects will end up hitting themselves? all i can see here is each enemy testing for collision with the lasers.

> I have enemy ships appear four at a time, however sometimes they appear on top of each other, how can I make a hitTest that will determine if objects within the same array are hitting each other

do you want to see if the enemy ships are hitting each other?

---

<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:** [May 18, 2008, 2:28pm UTC](https://forum.kirupa.com/t/hittest-on-object-within-the-same-array/260658/3 "2008-05-18T14:28:36Z")

</div>

■■■■, wrong hitTest, this is the one I was using

```auto
for (var enemyNum:int=enemies.length - 1; enemyNum >= 0; enemyNum--) {
				for (var enemyNum2:int=lasers.length - 1; enemyNum2 >= 0; enemyNum2--) {
					if (enemies[enemyNum].hitTestObject(lasers[enemyNum2])) {

```

---

<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:** [May 18, 2008, 2:49pm UTC](https://forum.kirupa.com/t/hittest-on-object-within-the-same-array/260658/4 "2008-05-18T14:49:02Z")

</div>

```auto
for (var enemyNum:int=enemies.length - 1; enemyNum >= 0; enemyNum--) {
                for (var enemyNum2:int=lasers.length - 1; enemyNum2 >= 0; enemyNum2--) {
                    if (enemies[enemyNum].hitTestObject(enemies[enemyNum2])) {

```

---

<div class="post-metadata">

**Author:** ![zanemx](https://avatars.discourse-cdn.com/v4/letter/z/46a35a/32.png) [@zanemx](https://forum.kirupa.com/u/zanemx)\
**Post date:** [May 18, 2008, 5:52pm UTC](https://forum.kirupa.com/t/hittest-on-object-within-the-same-array/260658/5 "2008-05-18T17:52:40Z")

</div>

Just an Idea

```auto

//-------COLLISION DETECTION
			for(var i:int = 0; i < enemies.length; i++){
				if(enemies*.hitTestObject(enemies[i + 1])){
					do whatever();
				}
			}

```

---

<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:** [May 18, 2008, 7:35pm UTC](https://forum.kirupa.com/t/hittest-on-object-within-the-same-array/260658/6 "2008-05-18T19:35:16Z")

</div>

thanks, Ill give it a try
