Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with multiple if statements

Status
Not open for further replies.

defkon2012

Programmer
Jan 16, 2002
18
0
0
US
I have a problem with this game I am making.

I need a better way to detect if a enemy ship has come into contact with one of the hero's bullets. Right now on every enemy I have a if statement that has way too many || or statements in it. I have 40 different friendly bullets that need to be checked for hitTest within my if statement.
So I have something that looks like this...

CODE FOR ENEMY SHIP
if(this.hitTest(bullet01)||this.hitTest(bullet02)||this.hitTest(bullet03)... ect ect
all the way to bullet40.

now when I have multiple enemys on screen all running this incredible if statement, I have a major problem with processor speed.

So my question is, would anyone know how to do this better? I have been thinking there is a way using the
substring() function. Using it to detect if the characters "bullet" is in a string. But I have had problems incorporating it with a if and hitTest statement.

does anyone have a suggestion on how to detect if a movieClip has come into contact with one of 40 other movieClips without running 40 if statements or ||?

thank you
defkon2012
 
You'll still need to use "ifs" but you could do this more efficiently with a loop...

for (i=1;i<=40;i++){
currentBullet=_root[&quot;bullet&quot;+i];
if(this.hitTest(currentBullet)){
//do stuff
}
}


...you'll have to rename &quot;bullet01&quot; to &quot;bullet1&quot; etc. for this to work either that or add an extra line which adds he zero by string concatenation but that's comparatively a very slow process.

Either way you still have to run hitTest 40 times which is pretty intensive - it might be an idea to check if the bullets are in the same general area as your target first and only hitTest the ones which are closest.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top