PDA

View Full Version : for loop



Stephen
February 7th, 2003, 05:09 AM
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

Stephen
February 7th, 2003, 05:36 AM
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

pom
February 7th, 2003, 09:08 AM
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:

senocular
February 7th, 2003, 10:24 AM
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);

kode
February 7th, 2003, 10:36 AM
Are you belittling ilyas, senocular?? :sleep: :P

pom
February 7th, 2003, 10:59 AM
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:

kode
February 7th, 2003, 11:13 AM
lol :P

:: kax pulls out his trout shield ::

senocular
February 7th, 2003, 11:13 AM
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:
sorry lost

lol


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

cybergold
February 7th, 2003, 11:14 AM
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

senocular
February 7th, 2003, 11:16 AM
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