# For loop

**URL:** <https://forum.kirupa.com/t/for-loop/13000>\
**Category:** flash\
**Created:** [February 7, 2003, 10:09am UTC](https://forum.kirupa.com/t/for-loop/13000 "2003-02-07T10:09:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Stephen](https://forum.kirupa.com/u/Stephen)\
**Post date:** [February 7, 2003, 10:09am UTC](https://forum.kirupa.com/t/for-loop/13000/1 "2003-02-07T10:09:02Z")

</div>

Hello,

I am writing a script to detect when I collide one movie clip with another and add numbers to the score. I have the script working and it looks like this.

if (\_root.cone1.hitTest(\_root.car)) {  
numHit +=1;  
}

I want to increase the numer of items on the screen and thought I would try a for loop for a number of instances of the cone object ie  
for (i=0; i\<10; i++)  
{  
if (\_root.cone1.hitTest(\_root.car)) {  
numHit +=1;  
}  
}

within this i need to replace cone1 with and identifier based upon the value of i. How do I do this and what is the syntax required.

Thanks for any help

---

<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:** [February 7, 2003, 10:36am UTC](https://forum.kirupa.com/t/for-loop/13000/2 "2003-02-07T10:36:03Z")

</div>

Solved it!!!

for (i=0; i\<10; i++){  
myCone = \_root[“cone”+i];  
if (myCone.hitTest(\_root.car)) {  
numHit +=1;  
}  
}

It works not sure if its the best way of doing it though

---

<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:** [February 7, 2003, 2:08pm UTC](https://forum.kirupa.com/t/for-loop/13000/3 "2003-02-07T14:08:50Z")

</div>

Yep, it’s the rigth way to do it. Good idea with the reference too 🙂 But you could declare it as a temporary variable (with “var”), as it is just used inside the loop.  
You could numHit++, by the way. It’s nicer :beam:

---

<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:** [February 7, 2003, 3:24pm UTC](https://forum.kirupa.com/t/for-loop/13000/4 "2003-02-07T15:24:43Z")

</div>

you could also put it all in one line (though its not really ‘nicer’ I dont think heh more confusing)

for (i=0; i\<10; i++) numHit += \_root[“cone”+i].hitTest(\_root.car);

---

<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:** [February 7, 2003, 3:36pm UTC](https://forum.kirupa.com/t/for-loop/13000/5 "2003-02-07T15:36:34Z")

</div>

Are you belittling ilyas, senocular?? :sleep: 😛

---

<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:** [February 7, 2003, 3:59pm UTC](https://forum.kirupa.com/t/for-loop/13000/6 "2003-02-07T15:59:45Z")

</div>

I’m not used to that boolean trick you use, Sen, but why not?

A tiny bit clearer way could be, I guess

```auto
for (i=0; i<10; i++) _root["cone"+i].hitTest(_root.car) ? numHit++ : null ; 

```

kax \>\> :trout: :skull: :run: :cyborg:

---

<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:** [February 7, 2003, 4:13pm UTC](https://forum.kirupa.com/t/for-loop/13000/7 "2003-02-07T16:13:23Z")

</div>

lol 😛

[size=1]:: kax pulls out his trout shield ::[/size]

---

<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:** [February 7, 2003, 4:13pm UTC](https://forum.kirupa.com/t/for-loop/13000/8 "2003-02-07T16:13:39Z")

</div>

no I imitated him 🙂

he gave a suggestion; I gave a suggestion (which wasnt based off his and made the code messier and less nice - nice is good though) 🙂

Ill belittle him now though

the ?: if else structure isnt really appropriate in replacing just an if statement as ?: relates to “if not one then another” - the null being an unnecessary addition. Instead it could be just

for (i=0; i\<10; i++) if (\_root[“cone”+i].hitTest(\_root.car)) numHit++;

which is actually better than my initial example because the addition is only executed when the hitTest is true and not for every loop of the for clause

but I still :love: ilyaslamasse emmensely  
:flower:  
[size=1]sorry lost[/size]

lol

[edit] That by the way is essensially what ilyaslamasse firs suggested with ++ just on onle line 🙂

---

<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:** [February 7, 2003, 4:14pm UTC](https://forum.kirupa.com/t/for-loop/13000/9 "2003-02-07T16:14:54Z")

</div>

sorry for posting here, i know it is not supposed to be posted here, but how can i get my footer up? Kirupa’s thread makes no sense, there is no link to Flash Footers at the top of any page… And if i post this in Help Forum, it will not be found  
:-\ sorry about the post

---

<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:** [February 7, 2003, 4:16pm UTC](https://forum.kirupa.com/t/for-loop/13000/10 "2003-02-07T16:16:44Z")

</div>

its at the top of the forums home page  
[http://www.kirupaforum.com/index.php](http://www.kirupaforum.com/index.php)

or be found directly, here:  
[http://www.kirupa.com/forums/footer.htm](http://www.kirupa.com/forums/footer.htm)
