-
[F8-Help] hitTest with undefined element
Hi,
I'm creating a shooting game in wich all the enemies are called enemy1, enemy2, enemy3, etc. (in one level I can have 100+ enemies) Now, I need to do a hitTest detecting if a bullet hits any of these enemies and remembers wich enemy has been hit, so this enemy can be hurt.
This code was working well (on the bullet mc):
Code:
onClipEvent (enterFrame) {
//supposing 10 is the maximum number of enemies
for(i=0;i<=10;i++){
if (this.hitTest(_root["enemy" + i]))
{_root["enemy" + i].gotoAndPlay("hurt");}
}
}
It works well if i <= "a small number" but if I put a big number like 100, it's too laggy, considering the bullets are duplicated and they all run this loop over and over. So I need another alternative.
Maybe if I create an array containing "enemy1", "enemy2", etc. and it could detect if the bullet hit any element in the array? but how do I do that?
Thank you.
-
Pumpkin Carving 2008
Create a new mc: enemies.
When creating enemies, attach them to the enemies mc.
Have each bullet check to see if it hits the enemies mc. If it does, then and ONLY then, cycle through the enemies array.
You can also optimize this by splitting the play area into columns, rows, quadrants, etc to cut down the checking algorithm.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|