# Call an Array From a hitTest?

**URL:** <https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175>\
**Category:** flash\
**Created:** [August 10, 2003, 11:54pm UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175 "2003-08-10T23:54:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yeldarb](https://avatars.discourse-cdn.com/v4/letter/y/51bf81/32.png) [@Yeldarb](https://forum.kirupa.com/u/Yeldarb)\
**Post date:** [August 10, 2003, 11:54pm UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/1 "2003-08-10T23:54:53Z")

</div>

can this be done? if so, how?  
i’m trying to make two duplicated movie clips check for any of the others  
ie bullet239 hitting badguy92  
thanks

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 10, 2003, 11:58pm UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/2 "2003-08-10T23:58:52Z")

</div>

```auto
myarray = [_root.baddy1, _root.baddy2, _root.baddy3, _root.baddy4, _root.baddy5]
for (i=0; i<myarray.length; i++) if (bullet.hitTest(myarray*)) { break; trace(myarray*+" is hit!") }

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 12:36am UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/3 "2003-08-11T00:36:51Z")

</div>

is there a way to not type \_root.baddy, just have the numbers, like baddy1-9999999

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 12:48am UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/4 "2003-08-11T00:48:22Z")

</div>

```auto
for (i=1; i<=99999999999999999999999999; i++) {
if(_root["baddy"+i].hitTest(bulletMC)) {
trace("hit!");
break;
}
}

```

OR

```auto
for (i=1; i<=99999999999999999999999999; i++) {
if(eval("baddy"+i).hitTest(bulletMC)) {
trace("hit!");
break;
}
}

```

the second example uses eval, it’s decprecated (The flash 5 way), and the first uses the flash mx way. The second example, though, is faster…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 1:21am UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/5 "2003-08-11T01:21:44Z")

</div>

whoa, whoa, whoa…i hope you’re not serious, ahmed. this thing’s gonna run forever.

Do i remember you asking this type of question before, yeildarb?

As much as possible, try to stay away from long for loops. Nobody’s gonna want to watch a flash movie that freezes their computer.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 11, 2003, 3:23am UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/6 "2003-08-11T03:23:15Z")

</div>

this would run slow? is there a way to do it w/o it running slow?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 18, 2003, 7:51am UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/7 "2003-08-18T07:51:02Z")

</div>

set ranges for your mcs… like set your bullets to go 1-100 and your badguys to go 101-200 and if bullets get to 100 reset the counter to 1 and if your badguy counter gets to 200 reset it to 101 and then in your for loop you can do

for(i=1;i\<=100;i++)  
{  
for(j=101;j\<=200;j++)  
{  
//if i hits j then do stuff  
}  
}

and to go even faster then make even smaller ranges like 1-50 and 51-100 or whatever you want… fastest i could see is setting the ranges so its just enough for stuff to always be on the screen… because when you reset your variables say when it gets to 100 you set it back to 1… if 1 is still on the screen its going to get written over and the old #1 will disappear

anyway hope it helps

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 18, 2003, 9:58am UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/8 "2003-08-18T09:58:17Z")

</div>

Shame on you Ahmed !!:bad: :bad: :bad:

Ofcourse there is only one method of doing this correctly:  
[AS]MovieClip.prototype.explode = function()  
{  
trace(“POW! (” + this + " exploded)");  
}

for (var i in \_root)  
{  
obj = \_root\*;  
if (typeof(obj) == “movieclip” && obj.\_name.substr(0,5) == “baddy”)  
{  
hit = obj.hitTest(bulletMC);  
//if (hit)  
obj.explode();  
}  
}[/AS]

If loops through all the object in the \_root.  
Then it checks if its a MovieClip, and if the name starts with “baddy”.

If a match has been found it checks if there is a collision with the bullet, and if so, it “explodes” the movieclip.  
(MovieClip.prototype.explode() currently only traces a confirm)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 18, 2003, 11:08pm UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/9 "2003-08-18T23:08:52Z")

</div>

nice… i didnt even think of that… thats a lot faster than my method… instead of checking every one against every other one… the game would run a lot faster…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 19, 2003, 12:53am UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/10 "2003-08-19T00:53:34Z")

</div>

but there is more than 1 bullet…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 19, 2003, 12:57am UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/11 "2003-08-19T00:57:37Z")

</div>

that AS is in the bullet right? with bulletMC = this?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 19, 2003, 7:48am UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/12 "2003-08-19T07:48:12Z")

</div>

yeah you put it in the main bullet that you will be duplicating and then each duped one will check itself against the enemies

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [August 19, 2003, 10:09am UTC](https://forum.kirupa.com/t/call-an-array-from-a-hittest/28175/13 "2003-08-19T10:09:54Z")

</div>

Very good … check my explaination here:

[http://www.kirupaforum.com/forums/showthread.php?s=&threadid=31973](http://www.kirupaforum.com/forums/showthread.php?s=&threadid=31973)
