For loop

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

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

Yep, it’s the rigth way to do it. Good idea with the reference too :slight_smile: 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:

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);

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

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

A tiny bit clearer way could be, I guess

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

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

lol :stuck_out_tongue:

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

no I imitated him :slight_smile:

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) :slight_smile:

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 :slight_smile:

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

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

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